私は数日間この問題を解決しようとしましたが、うまくいきませんでした。
私は 4 つのモデルを含む webapp に取り組んでいます。ユーザー、投票、議論、評価。評価モデルは、引数の評価を処理すると想定されています。
関連付けは次のとおりです。
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.id を渡すために、評価ビューで @argument にアクセスしようとしていることです。
_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 %>
これは問題なく動作しますが、部分的な im レンダリング (評価) は @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 クラスの文字列文字列補間でアクセスできるようにしたいと思います。
これを修正するためにいくつかのことを行いました: 投票コントローラーに before_action を追加しました。これで @argument インスタンス変数にアクセスできるようになったのですが、最初の引数だけを取ったようで、すべて同じ ID になっていました。
def set_argument
@argument = Argument.find_by(params[:id])
end
@argument インスタンス変数をフォームに含めるにはどうすればよいですか? argument.id と user.id を評価モデルに渡しますか? 現在のコードでは、次のエラーが表示されます: undefined method `humanize' for 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 %>