我试图将我的CRUD的索引页合并为一个作为主要主页,这是我尝试的但是它提出了一个底层方法每个错误,任何想法?还不确定如何在创建和删除后重定向到它。
<h1>Weather forecasts</h1>
<%= link_to 'Search by City', new_cityweather_path(@cityweathers) %>
<%= link_to 'Search by Postcode', new_postcodeweather_path(@postcodeweathers) %>
<h1>Listing Postcodeweathers</h1>
<table>
<thead>
<tr>
<th>Postcode</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @postcodeweathers.each do |postcodeweather| %>
<tr>
<td><%= postcodeweather.postcode %></td>
<td><%= link_to 'Show', postcodeweather %></td>
<td><%= link_to 'Edit', edit_postcodeweather_path(postcodeweather) %></td>
<td><%= link_to 'Destroy', postcodeweather, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<h1>Listing Cityweathers</h1>
<table>
<thead>
<tr>
<th>City</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @cityweathers.each do |cityweather| %>
<tr>
<td><%= cityweather.city %></td>
<td><%= link_to 'Show', cityweather %></td>
<td><%= link_to 'Edit', edit_cityweather_path(cityweather) %></td>
<td><%= link_to 'Destroy', cityweather, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
答案 0 :(得分:1)
您可以定义单独的控制器,可以说是主控制器,如:
class MainController < ApplicationController
def index
@postcodeweathers = PostCodeWeather.all
@cityweathers = CityWeather.all
end
end
然后,模板views/main/index.html.erb
可以使用partial作为:
<h1>Weather forecasts</h1>
<%= link_to 'Search by City', new_cityweather_path(@cityweathers) %>
<%= link_to 'Search by Postcode', new_postcodeweather_path(@postcodeweathers) %>
<%= render 'post_code_weathers/index' %>
<%= render 'city_weathers/index' %>
然后,部分views/post_code_weathers/_index.html.erb
将为:
<h1>Listing Postcodeweathers</h1>
<table>
<thead>
<tr>
<th>Postcode</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @postcodeweathers.each do |postcodeweather| %>
<tr>
<td><%= postcodeweather.postcode %></td>
<td><%= link_to 'Show', postcodeweather %></td>
<td><%= link_to 'Edit', edit_postcodeweather_path(postcodeweather) %></td>
<td><%= link_to 'Destroy', postcodeweather, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
而且,另一个部分views/city_weathers/_index.html.erb
是这样的:
<h1>Listing Cityweathers</h1>
<table>
<thead>
<tr>
<th>City</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @cityweathers.each do |cityweather| %>
<tr>
<td><%= cityweather.city %></td>
<td><%= link_to 'Show', cityweather %></td>
<td><%= link_to 'Edit', edit_cityweather_path(cityweather) %></td>
<td><%= link_to 'Destroy', cityweather, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
现在,您对postcodeweathers控制器的创建和删除操作可以是:
class PostCodeWeatherController < ApplicationController
def create
# create code goes here
redirect_to main_index_path
end
def delete
# delete code goes here
redirect_to main_index_path
end
end
cityweathers控制器中的相同类型的操作。
答案 1 :(得分:0)
您是否在相应控制器的索引方法中添加了附加变量?
(注意:这会将两个对象合并到PostCodeWeather的索引页中)
class PostCodeWeathersController < ApplicationController
def index
@postcodeweathers = PostCodeWeather.all
@cityweathers = CityWeather.all
end
end
这将允许您在索引视图中访问这两组记录。
在创建和删除后重定向到索引页面时,您应该能够在控制器中的所需方法中添加它。
def create
@cityweather = CityWeather.new(params[:city_weather])
if @cityweather.save
redirect_to action: "index"
end
end