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].
Related
Can anyone help me figure out why I am getting the following error?
When assigning attributes, you must pass a hash as an argument.
The form:
<%= form_for([current_user, current_user.suggestions.build]) do |f| %>
<%= f.label :category %>:
<%= f.select :category, ['Startup', 'Cars', 'Kids', 'Food', 'Other'] %> <br>
<%= f.text_area :suggestion %><br>
<%= f.submit 'Submit Suggestion' %>
<% end %>
SuggestionController:
def create
#suggestion = current_user.suggestions.create(suggestion_params)
end
private
def suggestion_params
params.require(:suggestion).permit(:suggestion, :category)
redirect_to shirts_path
end
ApplicationController:
helper_method :current_user
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
suggestion_params is returning the last line of the method redirect_to shirts_path, which current_user.suggestions.create can't accept because it isn't acceptable data.
The redirect should be in create method:
def create
#suggestion = current_user.suggestions.create(suggestion_params)
redirect_to shirts_path
end
private
def suggestion_params
params.require(:suggestion).permit(:suggestion, :category)
end
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.
Update: Zishe figured it out. Correct params.require code should be:
def adventure_params
params.require(:adventure).permit(:story, :choice, :parent_id, :user_id)
end
with parenthesis instead of a bracket, of course.
Original question:
I am making a form_for that should be submitting 4 attributes to the Adventure model. The adventure model has ancestry. I keep getting wrong number of arguments (4 for 1) based on my params.require method. When I change to requirements down to 1, I see that all the attributes are blank in my database. I know they are in the params but for some reason they are not being saved. Here is my code:
Form
<div class="form">
<%= form_for #adventure do |f| %>
<%= f.label :choice %>
<%= f.text_area :choice %>
<%= f.label :story %>
<%= f.text_area :story %>
<%= f.label :parent_id %>
<%= f.text_field :parent_id %>
<%= f.submit "Post"%>
<% end %>
</div>
Controller
class AdventuresController < ApplicationController
before_filter :authenticate_user!
def home
end
def index
end
def new
#parent_id = params[:parent_id]
#adventure = Adventure.new
end
def show
#adventure = Adventure.find(params[:id])
end
def create
#user = current_user
#adventure = current_user.adventures.build(adventure_params)
if #adventure.save
flash[:success] = "Adventure created!"
redirect_to #adventure
else
flash[:error] = "There was an error"
redirect_to adventures_path
end
end
private
def adventure_params
params.require(:adventure).permit[:story, :choice, :parent_id, :user_id]
end
end
Model
class Adventure < ActiveRecord::Base
has_ancestry
belongs_to :user
validates :user_id, presence: true
validates :parent_id, presence: true
end
I have no idea why I am getting wrong number of arguments since the attributes show up in the params.
Change permit[...] to:
.permit(:story, :choice, :parent_id, :user_id)