I'm trying to have a simple button to update for each Comment through a submit form. Here is my View code:
<% #comments.each do |comment| %>
<%= form_for comment, url: article_comment_path(comment.article, comment), method: :patch do |f| %>
<%= hidden_field_tag :update_time, Time.now %>
<%= f.submit "Confirm" %>
<% end %>
<% end %>
Comments Controller Update Action Code:
def update
#article = Article.friendly.find(params[:article_id])
#comment = #user.comments.find(params[:id])
if #comment.update(comment_params)
redirect_to #comments
else
render article_comments_path(#article)
end
end
private
def comment_params
params.require(:comment).permit(:date, :note)
end
With the code above, I'm getting this error:
param is missing or the value is empty: comment
- the error highlights the params.require line in the private declaration
Your issue here is pretty simple, look at your form, you do not have any :note so when you try to require :note in your params hash then you get that error because there is not :note key in your params hash, for get around this you have two options:
Create another params method and use it conditionally:
private
def comment_params
params.require(:comment).permit(:date, :note)
end
def comment_params_minimal
params.require(:comment).permit(:date)
end
and then use it conditionally in your update action:
def update
#article = Article.friendly.find(params[:article_id])
#comment = #user.comments.find(params[:id])
if params[:comment][:note].present?
use_this_params = comment_params
else
use_this_params = comment_params_minimal
end
if #comment.update(use_this_params)
redirect_to #comments
else
render article_comments_path(#article)
end
end
The other way is tu directly update your comment using params hash instead of whitelist them with comment_params so if params[:comment][:note].present? update in the normal fashion else, update only the date attribute directly: params[:comment][:date]
Hope this helps you.
You are submitting to the article comment path, but your form is for article(like in your code <%= form_for article) and not comment. Thus the params you should look for first is the article params[:article]. I think if you put a debugger like this
def update
debugger #<<<<<<<<<
#article = Article.friendly.find(params[:article_id])
#comment = #user.comments.find(params[:id])
if #comment.update(comment_params)
redirect_to #comments
else
render article_comments_path(#article)
end
end
You can then check the params that is submitting to your controller update action. And most likely you will find your comment params in your article params like
params[:article][:comment]
but I am just guessing here. With debugger and server log you can check exactly what param were submitted to your update action.
Related
I want to save parameters sent by different submit button each time so that I can distinguish if it's published or just saved.
In my view,
<%= form_for #post do |f| %>
...
<%= f.submit 'Draft', name: "status", value: "draft" %>
<%= f.submit 'Publish', name: "status", value: "publish" %>
<% end %>
When I submit with "draft" button, I got parameters as follows
"post"=>{"title"=>"bar", "body"=>"foo"},
"status"=>"draft",
"controller"=>"posts",
"action"=>"create"}
My controller
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:title, :body).merge(:status, params[:status])
end
Then I got errors,
wrong number of arguments (2 for 1)
What did I do wrong, and how do I pass draft/publish parameters to save in a database?
.merge takes only one argument, which is where the wrong number of arguments error is coming from. Try merging just params[:status].
I'm having trouble displaying my form at /users/2/friends/new. I'm receiving
undefined method `friends_path' for #<#<Class:0x21f0c14>:0x21ef364>
Here is the beginning of the form
<% form_for(#friend) do |f| %>
And the friends controller
def new
#user = User.find(params[:user_id])
#friend = #user.friends.build
end
This is the route
resources :users do
resources :friends
end
And the relevant path from "rake routes"
users/:user_id/friends/new(.:format) {:controller=>"friends", :action=>"new"}
Any help or insight is greatly appreciated. This is my first rails 3 app.
Try:
user_friends_path(#user)
It's because it's a nested resource:
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Update:
As for the form, you can do:
<%= form_for [#user, #friend] do |f| %>
I am trying to show a form on the contact page, but it is from another controller.
The current code results in "First argument in form cannot contain nil or be empty"
After searching it looks there is a problem with the local hash that is not passed.
How can I correctly pass the locals with this code so that it works?
inquiries_controller.rb
class InquiriesController < ApplicationController
def new
#inquiry = Inquiry.new
end
def create
#inquiry = Inquiry.new(params[:inquiry])
if #inquiry.deliver
render :thank_you
else
render :new
end
end
end
inquiries_form.html.erb
<%= form_for #inquiry do |f|
f.text_field :name
f.text_field :email
f.text_area :message
f.submit "Send"
end %>
static_pages\contact.html.erb
<%= render "inquiries/form",
:inquiry => #inquiry %>
HI try adding this to your StaticPages Controller
class StaticPagesController < ApplicationController
def contact
#inquiry = Inquiry.new
end
end
Its a very common mistake. Also I believe your form may also be wrong unless you are using a gem that allows for that type of form. Let me know if this will fix your error.
When you have a new and create action with the purpose of a simple form that does a calculation (tableless). How do you show what was entered in the text boxes after the create action redirect_to the the new action, so the user sees their existing values.
The controller looks like this
def new
#sum = Sum.new
#result = session[:result]
end
def create
#sum = Sum.new(sum_params)
if #sum.valid?
result = Sum.calculate_total(#sum.first_number, #sum.second_number)
session[:result] = result
redirect_to new_sum_path
else
session[:result] = nil
render action: 'new'
end
end
private
def sum_params
params.require(:sum).permit(:first_number, :second_number)
end
end
The view looks like this
<h1>Result = <%= result %></h1>
<%= form_for #sum do |f| %>
<%= f.label :first_number %>
<%= f.text_field :first_number, class: 'form-input', placeholder: '1' %>
<%= f.label :second_number %>
<%= f.text_field :second_number, class: 'form-input', placeholder: '2' %>
<%= f.submit %>
<% end %>
The model has a method for the calcuation
def self.calculate_total(a,b)
a + b
end
User enters numbers in 2 input text_fields (new action)
User hits submit (create action)
Redirects back to new action with results showing using session[:result]
The input text_fields are blank. I would like this to show the
numbers the was entered.
My questions:
When validation kicks in.. the text fields have the user inputted values.. is this because it is contained within the same action and a redirect loses the information?
To save the inputs when the user gets the results and redirects back to the new action I could store it in a session[:first_number] in the new and assign
it if it's valid in the create action, is this okay or is there an easier/better way?
When you redirect to new_sum_path it is forgetting the old sum. If you want to display the old values, you could pass the current #sum.id through:
redirect_to new_sum_path(old_sum_id: #sum.id)
And then in your new action have:
if(params[:old_sum_id])
#sum = Sum.find(params[:old_sum_id])
else
#sum = Sum.new
end
This way, form_for #sum should pick up on those existing values and fill them in as defaults.
Hello there i have been having problems trying to get my Name added onto my index.html via the new.html using form_for kindly look into the code below and correct my code please
My controller
def new
#post = Name.new
end
def create
#post = Name.new(post_params)
if #post.save
redirect_to names_path, notice: "your name was added"
else
render "new"
end
end
def post_param
params.require(:post).permit(:name)
end
My new.html.erb
<h1> Add your name </h1>
<%= form_for #post do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit "Add your name" %>
<% end %>
I am having trouble in this lower part when i click the f.submit (the button that appears in the htlm) it throws me the error.
Your new and create methods should look like this
def new
#post = Post.new #here
end
def create
#post = Post.new(post_params) #here
if #post.save
redirect_to names_path, notice: "your name was added"
else
render "new"
end
end
You are confused of using attribute names with model name.You should be looking at these Guides.It should help you.
Update
As you said you created a Name model,you shouldn't be creating an attribute which is same as your model name.
Solution
Either change your attribute name or model name to some other meaningful name.