I have tried to solve this issue for some days now, but without luck.
I am working on a webapp that contains 4 models; users, votes, arguments and rating. The rating model is suppose to take care of rating on the arguments.
The associations are:
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
My problem is that im trying to access the @argument in the ratings view, in order to pass the argument.id of the current argument thats being rated.
_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 %>
This works fine, but the partial im rendering (ratings) will not take the @argument instance variable.
_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 %>
I would like to have it in the form_for and being able to access it with the string string interpolation for the css class.
I did a few things in order to fix this: I added a before_action in the votes controller, where i set_argument. That made it possible to access the @argument instance variable, but it seems like it just took the first argument, so they all had the same ID.
def set_argument
@argument = Argument.find_by(params[:id])
end
How do i include the @argument instance variable in the form? Passing the argument.id and the user.id to the ratings model? The current code gives me this error: undefined method `humanize' for nil:NilClass
Also, when submitting the ratings form, theres an route error, which i am not sure why is there.
My routes file looks like this:
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
I hope someone is able to help me (or guide me in the right direction) Thanks in advance!
The _form for adding a argument to a vote:
<%= 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 %>
You actually are passing in the @vote
object and arguments are attached through ActiveRecord Associations.
This is how to pass the parameter...
<%= render 'ratings/rating', vote: @vote %>
Then if you need to pass the argument to another partial...
# no '@' since you got it from votes.
<%= render 'ratings/rating', argument: argument %>
Then lose your @
in the partial for arguments...
<%= 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| %>