I am a newbie to Ruby on Rails and have scoured Stackoverflow and the internet to the best of my abilities and am still stumped.
In my set-up, a Rating belongs_to a Product and has_many Comments. I'm using simple-form and trying to use nested fields_for to add RatingComment through the Rating form. Interestingly, when using the singular form of :rating_comment, the field is displayed. But as expected, I get the unpermitted parameter error when trying to save. When I use plural :rating_comments, the field disappears. This is similar to this SO posting, but adding #rating.rating_comments.build to new action still does not work for me. I've tried restarting the server many times, and even reset the database to no avail. Would appreciate any assistance as I've been struggling with this issue for the past few days.
Note: I've also taken out what I think is irrelevant code from the snippets below. If I need to show more information, please do let me know. Thanks in advance!
routes.rb
resources :ratings, only: [:new, :create, :edit, :update, :destroy] do
resources :rating_comments, shallow: true
end
rating.rb
class Rating < ActiveRecord::Base
belongs_to :product
belongs_to :user
has_many :rating_comments, foreign_key: "rating_id", dependent: :destroy
accepts_nested_attributes_for :rating_comments, reject_if: :all_blank
end
rating_comment.rb
class RatingComment < ActiveRecord::Base
belongs_to :rating
validates :rating_id, presence: true
end
ratings_controller.rb
class RatingsController < ApplicationController
before_action :signed_in_user, only: [:create, :new, :show]
def new
#product = Product.find(params[:id])
#rating = #product.ratings.new
#rating.rating_comments.build
end
def create
#product = Product.find(params[:product_id])
#rating = #product.ratings.build(rating_params)
#rating.user_id = current_user.id
...
private
def rating_params
params.require(:rating).permit(:user_id, :product_id, :rating, rating_comments_attributes: [:rating_id, :content])
end
end
ratings/_new_rating_form.html.erb
<%= simple_form_for([#product, #rating], html: { class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input :rating, collection: 1..10, as: :radio_buttons,
item_wrapper_class: 'inline', checked: 5 %>
<%= f.simple_fields_for :rating_comments do |rc| %>
<fieldset>
<%= rc.input :content, label: "Comments" %>
</fieldset>
<% end %>
<%= f.error :base %>
<%= f.button :submit, "Submit" %>
<% end %>
Very embarrassed to admit that it turns out that I had the view written in the wrong action. Once I put it in the new.html, it finally worked.
Related
I have a project with the below scenario
Three Objects:
Client:The real world client
User: The who uses the portal
Department: The Client for whome the user works (XYZ.com,ABC.com)
The signup page have: FirstName,username,password,ClientName,Email
Client have only one field value: The client Name
FirstName,username,password,ClientName,Email are of user
Now what I want is to create a Department with ClientName as the Name of the Department and associate the user with the ID of the department.
User have a field department_id
Even with your reformulation, it is still not clear. So I'll go with my own interpretation. ALso, you didn't specify if you were using Mongoid or ActiveRecord. I'll go with a somewhat Mongoid style as it produces clean modelisation, but feel free to replace with ActiveRecord stuff.
Models
class User
has_one :department
has_one :client
accepts_nested_attributes_for :client
accepts_nested_attributes_for :department
field :first_name
field :password
field :username
field :email
class Client
belongs_to :user
field :name
class Department
belongs_to :user
field :name
your_controller.rb
def new
#user = User.new
#user.client = Client.new
#user.department = Department.new
end
def create
if #user = User.create(user_params)
redirect_to #user
else
render 'new'
end
end
private
def user_params
# Set department.name as client.name
if params[:user] and params[:user][:department] and params[:user][:client]
params[:user][:department][:name] = params[:user][:client][:name]
end
params.require[:user].permit(
:first_name, :password, :username, :email,
client_attributes: [:id, :name],
department_attributes: [:id, :name]
)
end
_form.html.erb
<%= form_for :user do |user| %>
<%= user.text_field(:name) %>
...
<%= user.fields_for :client do |client| %>
<%= client.text_field(:name) %>
<% end %>
<%= user.fields_for :department do |department| %>
<p class="text-info">A department will be created with the same name as the client</p>
<% end %>
<% end %>
I'm using the sign-up and login that we built with the rails tutorial as a base for a Reddit clone that I’m making. As it stands the application is functioning properly apart from the user_id in comments table is blank when I make a comment, the link_id is present and correct so I can make comments on a link. The user_id in links table is also present and correct.
I'm fairly certain that the error i've made is in the create action of comments_controller.rb but it could also be my original migration. What's confusing me (as a novice) is I had this working in it's current form once before with rails 4.1.8 and device. However, using this approach with rails 4.2.1 using the rails tutorial as a base, it doesn't work. I'm a bit new here so I hope i've formulated the post correctly and given enough information so somebody could give me some pointers as to the problem
Comment Controller
before_action :logged_in_user, only: [:create, :destroy]
def create
#link = Link.find(params[:link_id])
#comment = #link.comments.create(params[:comment].permit(:link_id, :body))
#comment.user = User.find(current_user.id)
redirect_to link_path(#link)
end
def destroy
#link = Link.find(params[:link_id])
#comment = #link.comments.find(params[:id])
#comment.destroy
redirect_to link_path(#link)
end
private
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
Form
app/views/links/show.html.erb
<h2 class="comment-count"><%= pluralize(#link.comments.count, "comment") %></h2>
<%= render #link.comments %>
<%= form_for([#link, #link.comments.build]) do |f| %>
<%= render 'comments/fields', f: f %>
<%= f.submit "Save Comment", class: 'btn btn-primary margin-bottom-10' %>
<% end %>
Partials
app/views/comments/_comment.html.erb
<p class="comment_body"><%= comment.body %></p>
<p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago </p>
app/views/comments/_fields.html.erb
<%= render 'shared/comment_error_messages' %>
<%= f.label :body %>
<%= f.text_area :body, class: 'form-control' %>
Routes
config/routes.rb
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end
Models
app/models/link.rb
belongs_to :user
has_many :comments, dependent: :destroy
app/models/comment.rb
belongs_to :user
belongs_to :link
validates :body, presence: true
app/models/user.rb
has_many :links, dependent: :destroy
Migration
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.integer :link_id
t.text :body
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
add_index :comments, :link_id
end
end
Hopefully that’s a good enough post and I’ve included everything you need so somebody can help me out, It's a rookie error but i can't see it. thanks in advance.
Regards
As it stands, you are currently creating a comment without saving user_id for it. Try the below code
#comments_controller.rb
def create
#link = Link.find(params[:link_id])
#comment = #link.comments.new(params[:comment].permit(:link_id, :body))
#comment.user = User.find(current_user.id)
#comment.save
redirect_to link_path(#link)
end
I am using rails 4.0.4 with nested_form 0.3.2 to build an app that allows users to organize movies in lists.
I have these main models, a List model (I've excluded things such as validations):
class List < ActiveRecord::Base
belongs_to :user
has_many :list_movie_pairs
has_many :movies, :through => :list_movie_pairs
accepts_nested_attributes_for :list_movie_pairs, :allow_destroy => true
accepts_nested_attributes_for :movies, :allow_destroy => true
end
A Movie model:
class Movie < ActiveRecord::Base
has_many :list_movie_pairs
has_many :lists, :through => :list_movie_pairs
has_many :reviews
end
A ListMoviePair model for the many-to-many relationship:
class ListMoviePair < ActiveRecord::Base
belongs_to :list
belongs_to :movie
validates_presence_of :list_id, :movie_id
validates_uniqueness_of :movie_id, scope: :list_id
end
I am trying to build an interface for the user to add movies to a created list. These routes serve my purpose:
get "/users/:username/lists/:id/add" => "lists#add_movies", :as => :user_list_list_movie_pairs
post "/users/:username/lists/:id/add" => "lists#submit_movies"
These are the classes in my ListsController that should make this possible:
def add_movies
#pair = list.list_movie_pairs.new # "list" is a helper that returns the current list
end
def submit_movies
#list = current_user.lists.find(params[:id])
#pair = #list.list_movie_pairs.new(pair_params)
if #pair.save
redirect_to user_list_path(current_user.username, #list)
else
render :add_movies
end
end
def list_params
params.require(:list).permit(:name, :description, :private, \
list_movie_pairs_attributes: [:id, :list_id, :movie_id, :_destroy], \
movies_attributes: [:id, :title, :_destroy])
end
And this is the form in my view
<%= nested_form_for [current_user, list, #pair] do |f| %>
<%= f.fields_for :movies do |movie_form| %>
<%= movie_form.text_field :title %>
<%= movie_form.link_to_remove "Remove movie" %>
<% end %>
<%= f.link_to_add "Add movie", :movies %>
<% end %>
I get this error when trying to access the view:
Invalid association. Make sure that accepts_nested_attributes_for is used for :movies association.
Which pops at this line:
<%= f.link_to_add "Add movie", :movies %>
Note 1: I am using the Devise gem for users, hence the "current_user" helper;
Note 2: I have tried using both "movies" and "list_movie_pairs", i.e.:
f.fields for :list_movie_pairs
and
f.link_to_add "Add movie", :list_movie_pairs
in my view, neither association seems to work
Your code in the view should be like this
<%= nested_form_for [current_user, list, #pair] do |f| %>
<%= f.fields_for :movies do |movie_form| %>
<%= movie_form.text_field :title %>
<%= movie_form.link_to_remove "Remove movie" %>
<%= movie_form.link_to_add "Add movie", :movies %> #note the change here
<% end %>
<% end %>
Update
There are several issues in your code
1.In your List model,this line is not required
accepts_nested_attributes_for :movies, :allow_destroy => true #not requied
2.In your ListsController,you have this line
#pair = #list.list_movie_pairs.new(pair_params)
It should be
#pair = #list.list_movie_pairs.new(list_params) because you have list_params method not pair_params
Maybe I still do not understand how Rails 4 works with has_many and belongs_to association.
My form doesn't save the has_many relationship
Entry Model
class Entry < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers
end
Answer Model
class Answer < ActiveRecord::Base
belongs_to :entry
validates :content, presence: true
validates :entry_id, presence: true
end
Entry Controller
def create
#answer = Entry.create(params.require(:entry).permit(:survey_id, answers_attributes [:content, :entry_id]))
redirect_to '/'
end
Form
<%= form_for(:entry, :url => {:controller => 'entrys', :action => 'create'}) do |q| %>
<%= q.hidden_field :survey_id, :value => #survey.id %>
<%= q.fields_for :answer do |a| %>
<%= a.text_field :content %>
<% end %>
<%= q.submit :Save %>
<% end %>
Debug error
Parameters: {"utf8"=>"✓", "authenticity_token"=>"**********************=", "entry"=>{"survey_id"=>"1", "answer"=>{"content"=>"asdasd"}}, "commit"=>"Save"}
Unpermitted parameters: answer
(0.1ms) begin transaction
Thanks in advance.
Update fields_for in your form as below:
<%= q.fields_for :answers do |a| %>
<%= a.text_field :content %>
<% end %>
Note pluralized :answers and not :answer.
As Entry model is associated to Answer in a 1-M Relationship, you should be using plural :answers in fields for.
Right now as you used :answer (singular), it wasn't interpreted by rails correctly, so you received answer key in params hash instead of receiving answers_attributes which obviously resulted in warning as Unpermitted parameters: answer
UPDATE
Update the create action as below:
def create
#answer = Entry.create(params.require(:entry).permit(:survey_id, answers_attributes: [:content, :entry_id]))
redirect_to '/'
end
It should be answers_attributes: [:content, :entry_id] and not answers_attributes [:content, :entry_id] (Notice missing :)
Also, I would suggest you to update your Entry Controller new action as below:
def new
#entry = Entry.new
#entry.answers.build
end
After this update the form_for as below:
<%= form_for(#entry) do |q| %>
NOTE:
The controller name is not as per Rails convention so it is messing up the params hash. Instead of getting entry as a key, you are getting entrie. I would recommend changing the controller name EntrysController to EntriesController. Renaming the file entrys_controller.rb to entries_controller.rb. Also, update the routes specific to this controller in routes.rb by replacing all occurrences of entrys with entries
I am trying to update a nested form. The client table is updated successfully but the location table isn't update. Instead of its creating a new location. Do you guys have any solution ? I've already spend one day on it.
My models:
class Client < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations, :allow_destroy => true, :update_only => true
end
class Location < ActiveRecord::Base
belongs_to :client
end
Selectcom::Application.routes.draw do
resources :clients
end
My Controller:
class ClientsController < ApplicationController
before_action :set_client, only: [:edit, :update]
def index
#clients = Client.paginate(page: params[:page])
end
def new
#client = Client.new
end
def create
#client = Client.new(client_params)
if #client.save
flash[:success] = "Client added successfully"
redirect_to clients_path
else
render 'new'
end
end
def edit
end
def update
if #client.update(only_client_params)
flash[:success] = "Job updated successfully"
redirect_to clients_path
else
render 'edit'
end
end
private
def set_client
#client = Client.find(params[:id])
end
def client_params
params.require(:client).permit(
:name,
:phone,
:fax,
:url,
:address,
:city,
locations_attributes: [
:site,
:fax,
:phone,
:url,
:address,
:_destroy
]
)
end
end
This is the client's edit.html.erb form
<%= form_for(#client, class: 'form-horizontal') do |f| %>
<%= render(partial: 'client_field', locals: {f: f}) %>
<%= f.fields_for :locations do |l| %>
<%= l.hidden_field :client_id, value: #client.id %>
<%= l.hidden_field :_destroy %>
<%= l.text_field :site, class: 'form-control' %>
<% end %>
<%= f.submit "Save", class: "btn lg-button" %>
<% end %>
Actually, you have two issues here. One in your Model and other in your Controller permitted params. Let's dig into them:
1) Model
The update_only option is ignored when used with collection association (it is your case), as said in Rails documentation:
For a one-to-one association, this option allows you to specify how
nested attributes are to be used when an associated record already
exists. In general, an existing record may either be updated with the
new set of attribute values or be replaced by a wholly new record
containing those values.
By default the :update_only option is false and the nested attributes are used to update the existing record only if they include
the record's :id value. Otherwise a new record will be instantiated
and used to replace the existing one.
However if the :update_only option is true, the nested attributes are
used to update the record's attributes always, regardless of whether
the :id is present. The option is ignored for collection
associations.
So, the first step would be removing the update_only option of your Client class, because it will be ignored since you have a has_many association (collection association) with locations:
class Client < ActiveRecord::Base
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations, :allow_destroy => true
end
2) Controller
You have to permit the :id key for the :locations_attributes array. Since your update_only option in the model was ignored, Rails needs the param to tell that it is a record that exists and it's being updated rather than created.
You can solve your issue using the following in your client_params method (note the addition of the id key in your :locations_attributes key):
def client_params
params.require(:client).permit(
:name,
:phone,
:fax,
:url,
:address,
:city,
locations_attributes: [
:id, # Should be present; otherwise, Rails thinks that is a new record
:site,
:fax,
:phone,
:url,
:address,
:_destroy
]
)
end
I hope it helps !!