我有一个嵌套关系,其中仪表板有很多奖励,我正在尝试向页面添加fields_for以编辑奖励。不幸的是,它似乎没有起作用,我不知道为什么。
这就是我所拥有的。
仪表板型号:
class Dashboard < ActiveRecord::Base
belongs_to :manager
has_many :rewards
accepts_nested_attributes_for :rewards, allow_destroy: true
end
奖励模式:
class Reward < ActiveRecord::Base
belongs_to :dashboard
end
仪表板控制器:
class DashboardsController < ApplicationController
before_action :authenticate_manager!
# Requires user to be signed in
def index
@dashboards = Dashboard.all
end
def new
@dashboard = Dashboard.new
end
def edit
@dashboard = Dashboard.find(params[:id])
end
def create
@dashboard = Dashboard.new(dashboard_params)
@dashboard.save
if @dashboard.save
redirect_to dashboard_path(@dashboard)
else
render :action => new
end
end
def update
@dashboard = Dashboard.find(params[:id])
if @dashboard.update(dashboard_params)
redirect_to :action => :show
else
render 'edit'
end
end
def show
@dashboard = Dashboard.find(params[:id])
end
def destroy
@dashboard = Dashboard.find_by_id(params[:id])
if @dashboard.destroy
redirect_to dashboards_path
end
end
private
def dashboard_params
args = params.require(:dashboard).permit(:title, :description, :rewards, {rewards_attributes: [ :id, :title, :referralAmount, :dashboardid, :selected, :_destroy] } )
args
end
end
仪表板视图中的表单:
<%= form_for :dashboard, url: dashboard_path(@dashboard), method: :patch do |f| %>
<% if @dashboard.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@dashboard.errors.count, "error") %> prohibited
this dashboard from being saved:
</h2>
<ul>
<% @dashboard.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_field :description %>
</p>
<%= f.fields_for :rewards do |reward| %>
<%= reward.label :title %><br>
<%= reward.text_field :title %>
<%= reward.check_box :_destroy %>
<%= reward.label :_destroy, "Remove reward" %>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
我继续通过rails控制台手动将奖励添加到数据库,它工作得很漂亮,但它们没有显示在页面上。如果我像这样迭代它们,它们就会显示出来
<% if @dashboard.rewards.any? %>
<ul>
<% @dashboard.rewards.each do |reward| %>
<li><%= reward.title %></li>
<li><%= reward.referralAmount %></li>
<% end %>
</ul>
<% else %>
<p>no rewards</p>
<% end %>
但是,fields_for不显示奖励或其内容,因此可以编辑它们。
如果您需要更多信息/代码,请与我们联系。
答案 0 :(得分:1)
尝试修改您的:
查看强>:
<% if @dashboard.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@dashboard.errors.count, "error") %> prohibited
this dashboard from being saved:
</h2>
<ul>
<% @dashboard.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for @dashboard, url: dashboard_path(@dashboard) do |f| %>
........
<% end %>
控制器( has_many relat ionhip):
def new
@dashboard = Dashboard.new
@dashboard.rewards.build
end
private
def dashboard_params
params.require(:dashboard).permit(:title, :description,
rewards_attributes: [
:id,
:title,
:referralAmount,
:dashboardid,
:selected,
:_destroy
])
end
如果表单,您不必设置方法:patch 。 进入编辑页面后,Rails将在表单提交时使用控制器中的更新操作。
要检查它,请运行佣金路线, 你会看到像这样的somsthing:
PATCH /dashboards/:id(.:format) dashboards#update
PUT /dashboards/:id(.:format) dashboards#update
答案 1 :(得分:0)
尝试设置&#34; @奖励&#34;仪表板编辑方法中的实例变量(其中@rewards = @ dashboard.rewards)。然后用@rewards替换:奖励。
修改强>
我相信我的初步答案与您的确切问题相符(虽然说明显示特定信息中心及其奖励的页面会有所帮助)。以上答案在正确的轨道上重新开始:
添加@santosh dadi的建议
@dashboard.rewards.build
(假设您只需要一个表单上的一个奖励字段,用于&#34;新&#34;)
最后,为了避免为新的奖励表单制作虚假信息,请添加到Dashboards模型的顶部:
accepts_nested_attributes_for :rewards, reject_if: lambda {|attributes| attributes['title'].blank?}
http://guides.rubyonrails.org/form_helpers.html#nested-forms
答案 2 :(得分:0)
在控制器中,您需要提供构建
def new
@dashboard = Dashboard.new
@dashboard.rewards.build
end
&#34;建立&#34;只是在内存中创建一个新对象,以便视图可以获取此对象并显示某些内容,尤其是对于表单。
希望它对你有帮助
答案 3 :(得分:0)
您应该在嵌套表单之前构建对象。您可以添加任何您想要的对象。
在控制器中试用;
gulp.watch(['src/App/**/*.js', 'src/**/*.css'], ['inject']);