Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从两个视图创建对象_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 从两个视图创建对象

Ruby on rails 从两个视图创建对象,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想创建一个音乐会。这场音乐会有很多信息和它将要去的地方 在第一个视图中。我有一个创建新音乐会的表格(开始时间、乐队演奏、结束时间、价格)。在第二个视图中,我有一个存储在DB中的位置表。用户必须从现有位置中选择一个位置 然后,我将使用information+concert_id将concert对象存储在数据库中。我的问题是,我不知道该怎么做 编辑: 这是我想做的 concert form -> click submit -> redirect to locations dataTab

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

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

然后,我将使用information+concert_id将concert对象存储在数据库中。我的问题是,我不知道该怎么做

编辑: 这是我想做的

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
这必须重定向我以选择包含以下位置数据表的_concert_path.html.erb。这给我带来了一个问题,因为concert_id为nil。实际上,concert尚未保存在数据库中

 <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>

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

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

<%= 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--->

欢迎来到SO。我建议你阅读,这将帮助你写出好的问题(进而得到更好的答案)
<%= 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--->
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
<%= form_for @concert do |form| %>
   <!--- concert fields except for location --->
   <%= form.submit %>
<% end %>
<%= 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 %>
resources :concerts

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