【发布时间】: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
我希望这一次我解释得更清楚。
【问题讨论】:
-
欢迎来到 SO。我建议阅读stackoverflow.com/help/how-to-ask,这将帮助你写出好问题(并且——反过来——得到更好的答案)。
-
我还建议您在rubyonrails.org/documentation查看 Rails 文档本身
标签: ruby-on-rails ruby