将实例变量从另一个模型传递到表单中
passing instance variable from another model into a form
几天来我一直试图解决这个问题,但没有成功。
我正在开发一个包含 4 个模型的网络应用程序;用户、选票、论点和评级。评级模型应该负责对参数进行评级。
协会是:
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :argument
end
class Vote < ActiveRecord::Base
has_many :vote_options, dependent: :destroy
has_many :arguments
accepts_nested_attributes_for :vote_options, :reject_if => :all_blank, :allow_destroy => true
class Argument < ActiveRecord::Base
belongs_to :user
belongs_to :vote
has_many :ratings
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
has_many :uservotes, dependent: :destroy
has_many :vote_options, through: :uservotes
has_many :arguments, through: :votes
has_many :ratings
我的问题是我试图访问评级视图中的@argument,以便传递当前正在评级的参数的 argument.id。
_argument.html.erb
<% @vote.arguments.each do |argument| %>
<%= div_for(argument) do |argument| %>
<div class="col-md-4">
<div class="well clearfix">
<div class="col-md-12">
<h2><%= argument.title %></h2>
</div>
<div class="col-md-8">
<p><%= argument.argument %></p>
</div>
<div class="col-md-4">
<div class="col-md-12">
<%= image_tag argument.user.picture, class: 'img-responsive' %>
</div>
<div class="col-md-12">
<p>Af: <%= argument.user.first_name %></p>
</div>
</div>
<div class="col-md-12">
<%= render 'ratings/rating' %>
</div>
<% end %>
</div>
</div>
<% end %>
<% end %>
这很好用,但是局部即时渲染(评分)不会使用@argument 实例变量。
_rating.html.erb
<%= form_for ([@argument, @argument.ratings.new]), as: :post, url: new_rating_path(@argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
<fieldset class="rating">
<h1><%= @argument.id %></h1>
<%= f.radio_button :score, '5', class: 'star', id: "star5_#{@argument.id}", value: 5 %>
<%= f.label 'a', class: "full", for: "star5_#{@argument.id}" %>
<%= f.radio_button :score, '4', class: 'star', id: "star4_#{@argument.id}", value: 4 %>
<%= f.label '', class: "full", for: "star4_#{@argument.id}" %>
<%= f.radio_button :score, '3', class: 'star', id: "star3_#{@argument.id}", value: 3 %>
<%= f.label '', class: "full", for: "star3_#{@argument.id}" %>
<%= f.radio_button :score, '2', class: 'star', id: "star2_#{@argument.id}", value: 2 %>
<%= f.label '', class: "full", for: "star2_#{@argument.id}" %>
<%= f.radio_button :score, '1', class: 'star', id: "star1_#{@argument.id}", value: 1 %>
<%= f.label '', class: "full", for: "star1_#{@argument.id}" %>
</fieldset>
<div class="actions">
<%= f.submit 'Rate', class: 'btn btn-primary' %>
</div>
<% end %>
我想将它放在 form_for 中并能够使用 css class.
的字符串插值来访问它
我做了一些事情来解决这个问题:
我在投票控制器中添加了一个 before_action,其中我 set_argument。这使得访问 @argument 实例变量成为可能,但它似乎只接受了第一个参数,因此它们都具有相同的 ID。
def set_argument
@argument = Argument.find_by(params[:id])
end
如何在表单中包含@argument 实例变量?将 argument.id 和 user.id 传递给评级模型?
当前代码给我这个错误:
nil:NilClass
的未定义方法“人性化”
此外,在提交评分表时,出现路线错误,我不确定为什么会这样。
我的路线文件如下所示:
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks", registrations: 'users' }
devise_scope :user do
resources :users, only: [:show]
end
root 'votes#index'
resources :votes do
resources :arguments
end
resources :uservotes, only: [:create]
resources :ratings
我希望有人能够帮助我(或指导我正确的方向)
提前致谢!
为投票添加参数的_form:
<%= form_for([@vote, @vote.arguments.new]) do |f| %>
<p><%= f.label :Bruger %><br>
<%= current_user.first_name %>
<p><%= f.label :Titel %><br>
<%= f.text_field :title, placeholder: "Skriv en overskrift" %></p>
<p><%= f.label :Argument %><br>
<%= f.text_area :argument, cols: 35, rows: 6, placeholder: "Max 250 tegn" %>
<p><%= f.submit 'Opret', class: 'btn btn-primary' %></p>
<% end %>
您应该将参数作为局部参数传递给 _argument.html.erb 中的渲染部分:
<%= render 'ratings/rating', locals: { argument: argument } %>
那么你的 _rating.html.erb 部分将有:
<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
<fieldset class="rating">
<h1><%= argument.id %></h1>
<%= f.radio_button :score, '5', class: 'star', id: "star5_#{argument.id}", value: 5 %>
<%= f.label 'a', class: "full", for: "star5_#{argument.id}" %>
<%= f.radio_button :score, '4', class: 'star', id: "star4_#{argument.id}", value: 4 %>
<%= f.label '', class: "full", for: "star4_#{argument.id}" %>
<%= f.radio_button :score, '3', class: 'star', id: "star3_#{argument.id}", value: 3 %>
<%= f.label '', class: "full", for: "star3_#{argument.id}" %>
<%= f.radio_button :score, '2', class: 'star', id: "star2_#{argument.id}", value: 2 %>
<%= f.label '', class: "full", for: "star2_#{argument.id}" %>
<%= f.radio_button :score, '1', class: 'star', id: "star1_#{argument.id}", value: 1 %>
<%= f.label '', class: "full", for: "star1_#{argument.id}" %>
</fieldset>
<div class="actions">
<%= f.submit 'Rate', class: 'btn btn-primary' %>
</div>
<% end %>
编辑:因为它是渲染的唯一参数,您可以这样做:
<%= render 'ratings/rating', argument: argument %>
您实际上是在传递 @vote
对象,并且参数是通过 ActiveRecord 关联附加的。
这是如何传递参数...
<%= render 'ratings/rating', vote: @vote %>
然后如果你需要将参数传递给另一个部分...
# no '@' since you got it from votes.
<%= render 'ratings/rating', argument: argument %>
然后在部分参数中丢失你的 @
...
<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
几天来我一直试图解决这个问题,但没有成功。
我正在开发一个包含 4 个模型的网络应用程序;用户、选票、论点和评级。评级模型应该负责对参数进行评级。
协会是:
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :argument
end
class Vote < ActiveRecord::Base
has_many :vote_options, dependent: :destroy
has_many :arguments
accepts_nested_attributes_for :vote_options, :reject_if => :all_blank, :allow_destroy => true
class Argument < ActiveRecord::Base
belongs_to :user
belongs_to :vote
has_many :ratings
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
has_many :uservotes, dependent: :destroy
has_many :vote_options, through: :uservotes
has_many :arguments, through: :votes
has_many :ratings
我的问题是我试图访问评级视图中的@argument,以便传递当前正在评级的参数的 argument.id。
_argument.html.erb
<% @vote.arguments.each do |argument| %>
<%= div_for(argument) do |argument| %>
<div class="col-md-4">
<div class="well clearfix">
<div class="col-md-12">
<h2><%= argument.title %></h2>
</div>
<div class="col-md-8">
<p><%= argument.argument %></p>
</div>
<div class="col-md-4">
<div class="col-md-12">
<%= image_tag argument.user.picture, class: 'img-responsive' %>
</div>
<div class="col-md-12">
<p>Af: <%= argument.user.first_name %></p>
</div>
</div>
<div class="col-md-12">
<%= render 'ratings/rating' %>
</div>
<% end %>
</div>
</div>
<% end %>
<% end %>
这很好用,但是局部即时渲染(评分)不会使用@argument 实例变量。
_rating.html.erb
<%= form_for ([@argument, @argument.ratings.new]), as: :post, url: new_rating_path(@argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
<fieldset class="rating">
<h1><%= @argument.id %></h1>
<%= f.radio_button :score, '5', class: 'star', id: "star5_#{@argument.id}", value: 5 %>
<%= f.label 'a', class: "full", for: "star5_#{@argument.id}" %>
<%= f.radio_button :score, '4', class: 'star', id: "star4_#{@argument.id}", value: 4 %>
<%= f.label '', class: "full", for: "star4_#{@argument.id}" %>
<%= f.radio_button :score, '3', class: 'star', id: "star3_#{@argument.id}", value: 3 %>
<%= f.label '', class: "full", for: "star3_#{@argument.id}" %>
<%= f.radio_button :score, '2', class: 'star', id: "star2_#{@argument.id}", value: 2 %>
<%= f.label '', class: "full", for: "star2_#{@argument.id}" %>
<%= f.radio_button :score, '1', class: 'star', id: "star1_#{@argument.id}", value: 1 %>
<%= f.label '', class: "full", for: "star1_#{@argument.id}" %>
</fieldset>
<div class="actions">
<%= f.submit 'Rate', class: 'btn btn-primary' %>
</div>
<% end %>
我想将它放在 form_for 中并能够使用 css class.
的字符串插值来访问它我做了一些事情来解决这个问题: 我在投票控制器中添加了一个 before_action,其中我 set_argument。这使得访问 @argument 实例变量成为可能,但它似乎只接受了第一个参数,因此它们都具有相同的 ID。
def set_argument
@argument = Argument.find_by(params[:id])
end
如何在表单中包含@argument 实例变量?将 argument.id 和 user.id 传递给评级模型? 当前代码给我这个错误: nil:NilClass
的未定义方法“人性化”此外,在提交评分表时,出现路线错误,我不确定为什么会这样。
我的路线文件如下所示:
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks", registrations: 'users' }
devise_scope :user do
resources :users, only: [:show]
end
root 'votes#index'
resources :votes do
resources :arguments
end
resources :uservotes, only: [:create]
resources :ratings
我希望有人能够帮助我(或指导我正确的方向) 提前致谢!
为投票添加参数的_form:
<%= form_for([@vote, @vote.arguments.new]) do |f| %>
<p><%= f.label :Bruger %><br>
<%= current_user.first_name %>
<p><%= f.label :Titel %><br>
<%= f.text_field :title, placeholder: "Skriv en overskrift" %></p>
<p><%= f.label :Argument %><br>
<%= f.text_area :argument, cols: 35, rows: 6, placeholder: "Max 250 tegn" %>
<p><%= f.submit 'Opret', class: 'btn btn-primary' %></p>
<% end %>
您应该将参数作为局部参数传递给 _argument.html.erb 中的渲染部分:
<%= render 'ratings/rating', locals: { argument: argument } %>
那么你的 _rating.html.erb 部分将有:
<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
<fieldset class="rating">
<h1><%= argument.id %></h1>
<%= f.radio_button :score, '5', class: 'star', id: "star5_#{argument.id}", value: 5 %>
<%= f.label 'a', class: "full", for: "star5_#{argument.id}" %>
<%= f.radio_button :score, '4', class: 'star', id: "star4_#{argument.id}", value: 4 %>
<%= f.label '', class: "full", for: "star4_#{argument.id}" %>
<%= f.radio_button :score, '3', class: 'star', id: "star3_#{argument.id}", value: 3 %>
<%= f.label '', class: "full", for: "star3_#{argument.id}" %>
<%= f.radio_button :score, '2', class: 'star', id: "star2_#{argument.id}", value: 2 %>
<%= f.label '', class: "full", for: "star2_#{argument.id}" %>
<%= f.radio_button :score, '1', class: 'star', id: "star1_#{argument.id}", value: 1 %>
<%= f.label '', class: "full", for: "star1_#{argument.id}" %>
</fieldset>
<div class="actions">
<%= f.submit 'Rate', class: 'btn btn-primary' %>
</div>
<% end %>
编辑:因为它是渲染的唯一参数,您可以这样做:
<%= render 'ratings/rating', argument: argument %>
您实际上是在传递 @vote
对象,并且参数是通过 ActiveRecord 关联附加的。
这是如何传递参数...
<%= render 'ratings/rating', vote: @vote %>
然后如果你需要将参数传递给另一个部分...
# no '@' since you got it from votes.
<%= render 'ratings/rating', argument: argument %>
然后在部分参数中丢失你的 @
...
<%= form_for ([argument, argument.ratings.new]), as: :post, url: new_rating_path(argument), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>