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.
Related
in my rails 4 app, i have a user model that has_one profile model which also belongs_to the user model
this is my first time using a nested resource so im kinda lost to it
here how it goes
, a user creates an account,after succesful email authentication , user proceed to create a profile. Problem is, im getting error on using the build_association. Im not sure whether the problem is actually that or im getting null values on the params(which i think not)
in my routes.rb
resources :users do
resources :profiles
end
profiles view, new.html.erb
<%= form_for(#profile,url:{ :controller => 'profiles', :action => 'create'},html: {class: "ui large segment form error"}) do |f| %>
<%= render 'shared/form_error_messages', object: f.object%>
<div class="ui horizontal divider">Create a Profile</div>
<%= f.text_field :first_name, :placeholder => "First Name" , :required => true%>
<%= f.submit "Submit Profile", class: "ui submit button"%>
<% end %>
profiles_controller.rb
def new
#user = User.find(params[:user_id])
#profile = #user.build_profile
end
def create
#profile = #user.build_profile(profile_params)
if #profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
private
def profile_params
params.require(:profile).permit(:first_name)
end
when im submitting the form, im getting
NoMethodError in ProfilesController#create
undefined method `build_profile' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:9:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MFCANYra..",
"profile"=>{"first_name"=>"sample",
"commit"=>"Submit Profile",
"user_id"=>"1"}
Try changing the create action in profiles_controller.rb as below :-
def create
#user = User.find(params[:profile][:user_id])
#profile = #user.build_profile(profile_params)
if #profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
let me know this worked or not, try to find if something else is causing the problem, thanks.
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 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.
Basic rails question..I have the following: a user has many packs and a pack belongs to a category. In the new pack form I want make a select field with the pack's categories. I have the following code :
def new_pack
#pack=Pack.new()
#user= User.find(params[:id])
#categories = Category.all
end
def create
#pack=Pack.new(pack_params)
#user= User.find(params[:user_id])
if #pack.save
#user.packs << #pack
flash[:notice]="Thank you!"
redirect_to(:action=>'attempt_activation', :id=> #pack.id)
else
render :action=> "new_pack", :id=>#user.id
end
end
And in my view:
<%= form_for(:pack, :url=>{:controller=> "packs", :action=>'create', :user_id=> #user.id}) do |f| %>
<h5>Registration takes less than 2 minutes.</h5>
<label>Title<b style="color:red;">*</b>
<%= f.text_field(:title, :placeholder=>"") %>
</label>
<label>Category<b style="color:red;">*</b>
<%= f.select(:category_id, #categories.map {|s| [s.title, s.id]}, :selected=> #categories.second) %>
...
THE PROBLEM is that if there are errors at the form and and the new_pack action has to be rendered again it throws an error:
ActionView::Template::Error (undefined method `map' for nil:NilClass):
</div>
<div class="large-4 columns">
<label>Category
<%= f.select(:category_id, #categories.map {|s| [s.title, s.id]}, :selected=> #pack.category) %>
</div>
Why is this happening? render is used to render a particular view using the instance variables available in the action but here is not loading again the #categories.
Thank you.
Add #categories = Category.all in create method:
def create
#pack = Pack.new(pack_params)
#user = User.find(params[:user_id])
if #pack.save
#user.packs << #pack
flash[:notice] = "Thank you!"
redirect_to(:action => 'attempt_activation', :id => #pack.id)
else
#categories = Category.all
render :action=> "new_pack", :id => #user.id
end
end
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)