so i know that there´s a lot of questions like that, but none had answered my questions and solved my problem. So, I am with this problem for about a week and I can´t solve it! I am really new to ruby on rails, but i´ve tried tried everything. I have this (ActionController::ParameterMissing:
param is missing or the value is empty:opinion) and I don´t know how to fix it. There´s all the code (I am newbie so it is really simple) :
Opinion Controller :
class OpinionsController < ApplicationController
def new
#opinion = Opinion.new
end
def create
#opinion = Opinion.new(opi_params)
#opinion.save
redirect_to #opinion
end
def show
#opinion = Opinion.find(params[:id])
end
private
def opi_params
params.require(:opinion).permit(:body)
end
end
New :
<h1>Opinions</h1>
<%= form_for :opinion do |f| %>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<%= f.submit "Create an option" %>
<% end %>
DB :
class CreateOpinions < ActiveRecord::Migration
def change
create_table :opinions do |t|
t.string :body
end
end
end
Show :
<h1>Your Opinions:</h1>
<div>
<%= #opinion.body %>
</div>
PLEASE HELP ME! I am getting crazy because i cannot solve it! Thanks :)
<h1>Opinions</h1>
<%= form_for #opinion do |f| %>
<%= f.label :body %><br>
<%= f.text_field :body %><br>
<br>
<%= f.submit "Create an option" %>
<% end %>
Change :opinion to the instance variable #opinion.
This issue may also occur when the form is submitted with the field body empty if it is your case change the strong parameters to
params.fetch(:opinion, {}).permit(:body)
I recommend dropping a debugger like pry or byebug right between def create and #opinion = Opinion.new(opi_params) and seeing what params is coming in. You might be missing opinion, which will trigger the ParameterMissing error.
Also, are you getting this error through the browser or through your test? If you are getting this error from your test, it might be your set up.
In the meantime, try params.permit(:body) in your opi_params method.
Related
I'm trying to submit a form in ruby on rails that i made, but keep getting de next error.
Ruby on Rails form: param is missing or the value is empty
my form
<%= form_for #test do |f| %>
<div class="field">
<%= f.text_field :first_name %><br>
<%= f.text_field :last_name %><br>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
my controller
def new
#test = Test.new
end
def create
#test = Test.new(allow_params)
if #test.save
redirect_to 'test/index'
else
render 'test/new'
end
end
private
def allow_params
params.require(:last_name).permit(:first_name)
end
my routes
resources :test
get 'test/index'
get 'test/new'
get 'test/create'
post '/tests' => 'test#create'
Your attributes are within the testlabel, so here you should go :
def allow_params
params.require(:test).permit(:first_name, :last_name)
end
Look, this is what you form posts when you click submit:
{"utf8"=>"✓","authenticity_token"=>"...", "test"=>"first_name"=>"poldo", "last_name"=>"de poldis"},"commit"=>"Create"}
As you can see first_name and last_name are inside an hash as value of a key called test. Indeed your function allow_params expects something like this
last_name: {first_name: 'poldo'}
as you can see the param (last_name) is missing, because is inside test!
The right way is as Ben answered:
params.require(:test).permit(:first_name, :last_name)
To understand better how strong parameters works I suggest to you to check this page Api doc or even better The latest version ofthe official manual
I already know there is a lot of undefined method questions, But I can't see whats wrong with mine so I need some help!
heres my form that i have
<% title("Home Page") %>
<h1><i class="fa fa-home"></i> Add Event <small>views/pages/home.html.erb</small></h1>
<div class="row">
<%= simple_form_for Newevent.new do |f| %>
<%= f.input :eventname, required: true %>
<%= f.input :eventdesc %>
<%= f.input :eventdate %>
<%= f.input :eventimage %>
<div class="col s6">
<%= f.input :stubhublink %>
<%= f.input :seatwavelink %>
<%= f.input :viagogolink %>
</div>
<%= f.button :submit %>
<% end %>
</div>
I also have this in the controller
def create
create_params = params[:newevent].permit(:eventname, :eventdesc, :eventdate, :eventimage, :viagogolink, :seatwavelink, :stubhublink)
#newevent = Newevent.new(create_params)
byebug
#newevent.save!
end
and this in the model
class Newevent < ActiveRecord::Base
def params
params.require(:newevent).permit(:eventname, :eventdesc, :eventdate, :eventimage, :viagogolink, :seatwavelink, :stubhublink )
end
end
It was working fine with 3 (which were eventname, eventdate and eventimage) however after adding the other 4 in it now doesn't work. Any ideas?
Sorry heres the error!
NoMethodError in Newevents#new
Showing /Users/samroberts/Desktop/admitme/app/views/newevents/new.html.erb where line #6 raised:
undefined method `eventdesc` for #<Newevent:0x007fcaeee41f68>
Sam
Several problems here buddy:
1. eventdesc Missing
The simple explanation for your error is that your eventdesc attribute is missing from your NewEvent model's table.
Since we don't have your table - or migrations - to observe, I'll have to give you a simple test:
<%= simple_form_for Newevent.new do |f| %>
<%= f.input :eventname, required: true %>
<%= f.input :eventdesc %> #-> remove this line
...
Remove that, refresh the page and see if it works. If the error persists, it may mean another attribute is missing. If it disappears, it means this attribute is missing from your db, and as such you should use rails g migration to create a new one:
$ rails g migration AddEventDesc
#db/migrate/add_event_desc_xxxxxxxx.rb
class AddEventDesc
def change
add_column :new_events, :eventdesc, :string
end
end
2. Params
Part of Rails 4's infrastructure is to use strong params to pass data from your controller to model.
As such, you need the following syntax:
#app/controllers/new_events_controller.rb
class NewEventsController < ApplicationController
def new
#newevent = NewEvent.new
end
def create
#newevent = NewEvent.new new_event_params
#newevent.save
end
private
def new_event_params #-> you can call this whatever you like
params.require(:newevent).permit(:eventname, :eventdesc, :eventdate, :eventimage, :viagogolink, :seatwavelink, :stubhublink )
end
end
You need to keep your strong params function in your controller, not your model.
3. Model Name
Finally, this won't do much for you directly, but in the long run will help you massively; your model name.
Your model is NewEvent.
If this is what you want to call it, that is good. However, I have found the best way forward with model names is to keep them simple, preferably to one word.
I'd strongly recommend calling your model Event, so you can call #event = Event.new etc -- it just flows better.
I am new to Rails, and just working my way through my first solo project, but I seem to be running into a syntax error with a constant not being initialized (Ive gotten several of these, but each seems to have a different cause.....not quite sure how i keep getting the same error with different causes :)):
uninitialized constant DatastringsController::Datastrings
DatastringsController:
class DatastringsController < ApplicationController
def new
end
def create
#datastrings = Datastrings.new(datastrings_params) #ERROR returned on this line
#datastrings.save
redirect_to #datastrings
end
def show
#datastrings = Datastrings.find(params[:id])
end
private
def datastrings_params
params.require(:datastrings).permit(:title, :text)
end
end
I believe my form is correct:
<%= form_for :datastrings, url: datastrings_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The main problem you have is here:
<%= form_for :datastrings, url: datastrings_path do |f| %>
form_for really should be populated with an ActiveRecord object, as this allows Rails to build the relative paths it requires correctly.
Although I don't know why this is the case, your current setup is basically trying to render DatastringsController::Datastrings -- primarily because you've not set up your form_for correctly
--
Fix
If you want to create a datastring object, I'd follow convention and do this:
#config/routes.rb
resources :datastrings
#app/controllers/datastrings_controller.rb
Class DatastringsController < ApplicationController
def new
#datastring = Datastring.new
end
def create
#datastring = Datastring.new(datastring_params)
#datastring.save
end
private
def datastring_params
params.require(:datastring).permit(:title, :text)
end
end
#app/views/datastrings/new.html.erb
<%= form_for #datastring do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
--
YOU ALSO NEED TO NAME YOUR MODELS IN SINGULAR
Looking at it now, it seems that your main issue is likely that you've named your model as a plural.
The reason this will be bad is that when you load Rails, it will load all your models, and consequently, allow you to call them by referencing their class name. If a model is plural, I don't think it will load it correctly, causing the error you've highlighted
If you name your model to the following, it should be better:
#app/models/datastring.rb
Class Datastring < ActiveRecord::Base
end
I am making a rails application. After a user has registered (I have already created user registration with devise), they can fill out this form that will contain their profile information. I have done this several times, and i can't find what is wrong. Here is the model:
class Information < ActiveRecord::Base
belongs_to :user
end
Here is the controller:
class InformationsController < ApplicationController
def new
#information = Information.new
end
def create
#information = Information.create(params[:information])
redirect_to student_path
end
def index
end
end
And here is the view for the new action.
<div class="span6 offset3 text-center">
<h1>Edit your information</h1>
<%= simple_form_for #information do |f| %>
<%= f.input :skills %>
<%= f.input :looking_for, :label => 'What help do you need?' %>
<%= f.input :my_idea %>
<%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
<% end %>
</div>
Here is the line in the routes file:
resources :informations
I get the following errors which make no sense to me:
undefined method `information_index_path' for #<#:0x007f9c00c7b3e0>
Does anyone know how to fix this? Thanks.
UPDATE:
When I did rake routes, For informations#create, which is what the form should be going to, it has a blank path. There is also informations#index, which is what I guess its going to now. How do I get it to go to informations#create if the path is blank?
Please try yanking out the comments (# signs) in lines 6 and 9 of your view. They might be messing up the ERB processing.
Can you try informations_path? See here.
The problem was with naming the resource information. As information is the same plural as it is singular, it was confusing rails. I renamed the model description and the controller descriptions_controller, and it worked.
I have a problem I can not resolve on a form
Here's my view:
<h1>create manager </h1>
<% form_tag :action => 'create_manager' do %>
<%= text_area :user, :nom %><br/>
<%= date_select :user, :date_embauche %>
<%= submit_tag "Submit" %>
<% end %>
and here is my controller:
def create_manager
tmp = params[:nom]
p(tmp)
render :partial => "adminpartial"
end
The problème is that params[:nom] return alltime nil.
I think i'm not using correctly the params variable.
Does anyone have an idea about this?
Did you look at the params passed in your logs?
I guess you might find something in params[:user][:nom]as explained here.
BTW, did you carefully read this?