【问题标题】:creating an object from two views从两个视图创建对象
【发布时间】:2015-08-27 15:37:43
【问题描述】:

我想创建一个音乐会。这场音乐会有很多信息和一个地方。

在第一个视图中。我有一个创建新音乐会的表格(开始时间、乐队演奏、结束时间、价格)。在第二个视图中,我有一个存储在数据库中的位置表。用户必须从现有地点中选择一个地点。

然后我会将带有信息+concert_id 的音乐会对象存储在数据库中。我的问题是我不知道该怎么做。

编辑: 这就是我想做的事

concert form -> click submit -> redirect to locations dataTable 
and choose one location-> get the location_id and add it to my instance of       concert -> click submit 
-> save in the database

所以,我用提交按钮创建了 new_concert.html.erb,一旦点击,创建动作就会被执行 创建动作定义如下

def create
@concert =current_user.concerts.build(concerts_params)
respond_to do |format|
  format.html { redirect_to choose_location_path(@concert), 
  notice: 'Vous participez a ce concert.' }
end
end

这必须将我重定向到包含位置数据表的 choose_concert_path.html.erb,如下所示。这给我带来了一个问题,因为 Concert_id 为 nil。事实上,音乐会还没有保存在数据库中。

 <div class="datatable">

                            <table class="table table-bordered table-hover"data-provides="rowlink">
                                <thead>
                                    <tr>
                                        <th align="center">Nom</th>
                                        <th align="center">Type</th>
                                        <th align="center">Pays</th>
                                        <th align="center">Ville </th>
                                        <th align="center">Quartier</th>
                                        <th align="center">Détail</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <% Location.all.each do |location| %>


                                    <tr >
                                                <td align="center">

                                        <%= location.name %></a></td>
                                        <% if location.type==true %>
                                        <td align="center">
                                        hall
                                        </td>
                                        <% else %>
                                        <td align="center">
                                        stadium
                                        </td>
                                        <%end%>

                                        <td align="center"><%= location.country %></td>
                                        <td align="center"><%= location.city %></td>
                                        <td align="center"><%= location.adress%></td>

                    <td align="center"><%=link_to "<i class='icon-link'></i> Choisir".html_safe, choose_location_path(location) ,method: :put, class: "btn btn-sm btn-success"  %></td>
                                    </tr>


<% end %>
                                </tbody>

                            </table>
                        </div>

选择位置后,我必须单击一个按钮,将我的 Concert_params+location_id 保存在数据库中。 总之,我的两个问题是:

 1- passing the @concert from one view to another
 2- get the id of selected location_id and add it to the other informations

我希望这一次我解释得更清楚。

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

这是一个非常广泛的问题。我知道您请求了两个视图,但我认为单选按钮是解决此问题的最佳方法,因此我的示例将全部集中在一个视图中。您可以应用它来使用多个视图。 这是一个带有单选按钮的表单:

<%= form_for @concert do |form| %>
<!--- insert the concert fields up here --->
<p>Concert Location</p>
<%= form.radio_button :career_goal, "location_one" %>
<%= form.label :concert_location, "Location One", value: "location_one" %></br>

<!--- add the rest of your options the same way as the above two lines--->

concert_location 只是一个字符串,它将采用您在单选按钮的字符串参数中提供的值。如果需要,您也可以将图像等放在单选按钮中。如果您还有其他问题,请告诉我。

编辑* 我决定我没有充分回答,因为您确实要求提供两个视图,所以就这样吧。

concerts_controller.rb:

class ConcertsController < ApplicationController
   def new
      @concert = Concert.new
   end

   def create
      @concert = Concert.new(concert_params)
      if @concert.save
         redirect_to concert_location_path(@concert.id)
      else
         #whatever you want to do upon failure
      end
   end

   def locations_list
      @concert = Concert.find_by_id(params[:id])
      @locations = Locations.all
   end

   def add_location
      @concert = Concert.find_by_id(params[:id])
      if @concert.update_attributes(concert_location_params)
         #wherever you want to take them upon success
      else
         #whatever you want to do on failure
      end
   end

   private
   def concert_params
      params.require(:concert).permit(:first_param,...)
      #don't add concert_location here
   end

   def concert_location_params
      params.require(:concert).permit(:concert_location)
   end
end

new.html.erb:

<%= form_for @concert do |form| %>
   <!--- concert fields except for location --->
   <%= form.submit %>
<% end %>

concert_list.html.erb:

<%= form_for @concert, :url => add_location_path(@concert.id), :method => "patch" do |form| %>
   <p>Concert Location</p>
   <%= @locations.each |location| do %>
      <%= form.radio_button :concert_location, location.id %>
      <%= form.label :concert_location, Location.name, value: "location.id" %></br>
   <% end %>
   <%= form.submit %>
<% end %>

将此添加到您的 routes.rb:

resources :concerts

get '/concerts/location_list/:id' => 'concerts#locations_list', as: :concert_location
patch '/concerts/add_location/:id' => 'concerts#add_location', as: :add_location

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2017-06-19
    相关资源
    最近更新 更多