When I submit the form below the "published" check_box value is not posting.
params3 = {"utf8"=>"✓",
"authenticity_token"=>"i4SbblLJKIwba9yD30sDQCsir28/xdUxQZ90qYTNn0A=",
"story"=>{"name"=>"asdsaddsad", "post"=>"asdasdasdasd",
"user_id"=>"13", "image_id"=>"1", "published"=>"1"}, "commit"=>"Save
Story", "action"=>"create", "controller"=>"stories"}
def create
#story = Story.new(story_params)
respond_to do |format|
if #story.save
format.html { redirect_to #story, notice: 'Story was successfully created.' }
format.json { render action: 'show', status: :created, location: #story }
else
format.html { render action: 'new' }
format.json { render json: #story.errors, status: :unprocessable_entity }
end
end
end
<%= form_for #story do |f| %>
<%= f.text_field :name, placeholder: "Enter Title" %>
<%= f.text_area :post, placeholder: "Enter Story" %>
<br/>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :image_id, value: #image.id %>
<%= f.label "Publish this" %>
<%= f.check_box :published %>
<%= f.submit "Save Story" %>
<% end %>
The data that is being passed to the action is story as seen in the params3 hash. Also, the published check box is being posted. Check boxes by default will pass a 1 or 0 to denote true / false. Rails will update the value accordingly and accept 1 or 0 for the value of a checkbox:
params3 = {"utf8"=>"✓", "authenticity_token"=>"i4SbblLJKIwba9yD30sDQCsir28/xdUxQZ90qYTNn0A=", "**story**"=>{"name"=>"asdsaddsad", "post"=>"asdasdasdasd", "user_id"=>"13", "image_id"=>"1", "published"=>"1"}, "commit"=>"Save Story", "action"=>"create", "controller"=>"stories"}
Therefore your object creation will need to use those params. Since you are using Rails 4, you will need to use strong_parameters which seems that you are. You will need to verify that published is an allowed value in your params hash.
def story_params
params.require(:story).permit(...., :published, ...)
end
Related
I'm writing a CMS and I faced a problem while making a role management for users. I have a boolean field :admin in my User model, and I've made a checkbox in my form to set created user as an administrator. Here is the users_controller:
def create
#user = User.create(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :new }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :edit }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
and this is my form :
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, '' %>
# I also tried with <%= f.hidden_field :admin, false %>
<%= f.check_box :admin, checked = true %>
# Or <%= f.check_box :admin, data: { switch: true } %>
</p>
<% end %>
But any of these options returns me the following:
NoMethodError in Multiflora::Users#edit
undefined method `merge' for "":String
What have I done wrong?
Take hidden field as
<%= f.hidden_field :admin, value: '' %>, or simply
<%= f.hidden_field :admin %>
and checkbox as <%= f.check_box :admin, :checked => true %>
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, value: '' %>/<%= f.hidden_field :admin %>
<%= f.check_box :admin, :checked => true %>
</p>
<% end %>
I get a ForbiddenAttributesError when I send an update to my Rails 4 controller.
The model is 'Company' and it has a private method in it's controller:
def company_params
params.require(:company).permit( :adress_id,
:name,
:zusatz,
:kontakt,
:strasse,
:adresszusatz,
:plz,
:ort,
:telefon,
:fax,
:natel,
:email,
:alternative_email,
:url,
:anbieter_id,
:eintrittsdatum,
:betrag,
:bemerkungen,
:betrag_gwrj,
:betrag_sgkv,
:rechnungszusatz,
:zusatzfeld_7,
:zusatzfeld_8,
:zusatzfeld_9,
:zusatzfeld_10,
:datum_mutation,
:verzeichnis_id,
:industry_ids => []#,
#:latitude,
#:longitude
)
end
BetterErrors shows me these Request parameters:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"0rDHB7BNuHikL3/Fktdaj6BFFDinpUwPdpy+12HdMw4=", "company"=>{"adress_id"=>"", "name"=>"AGIP Tankstelle Rapperswil", "zusatz"=>"", "kontakt"=>"Eni Suisse S.A.", "strasse"=>"Zürcherstrasse 92", "adresszusatz"=>"", "plz"=>"8640", "ort"=>"Rapperswil", "telefon"=>"", "fax"=>"", "natel"=>"", "email"=>"", "url"=>"", "anbieter_id"=>"", "eintrittsdatum"=>"", "betrag"=>"", "bemerkungen"=>"", "betrag_gwrj"=>"", "betrag_sgkv"=>"", "rechnungszusatz"=>"", "zusatzfeld_7"=>"test", "zusatzfeld_8"=>"", "zusatzfeld_9"=>"", "zusatzfeld_10"=>"", "datum_mutation"=>"", "verzeichnis_id"=>"HR & Stadt", "alternative_email"=>""}, "commit"=>"Speichern", "action"=>"update", "controller"=>"companies", "id"=>"375"}
The form looks like this:
...
<%= form_for #company, url: {action: "update"}, html: {class: "form-horizontal"} do |f| %>
<div class="col-md-6">
<div class="form-group">
<%= f.label :adress_id %>
<%= f.text_field :adress_id %>
</div>
...
<%= f.submit "Speichern" %>
</div>
<% end %>
And this is the Update Method in the controller:
def update
#company = Company.find(params[:id])
if #company.update_attributes(params[:company])
redirect_to(#company)
else
render :edit
end
end
If I use the "company" hash in the rails console and create a company via Company.create [hash], it works fine. Any idea why Rails is throwing this error at me?
Be sure than in your controller you're using company_params in update_attributes:
def update
#company = Company.find(params[:id])
respond_to do |format|
# Here use company_params and not params[:company]
if #company.update_attributes(company_params)
format.html { redirect_to #company, notice: 'company updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
I am trying to submit a form and getting this error: Couldn't find Event without an ID
Here is the controller:
def create
#event = Event.find(params[:event_id])
#event_sponsorship = EventSponsorship.new(params[:event_sponsorship])
#event_sponsorship.sponsor_id = current_user.id
#event_sponsorship.event_id = #event
respond_to do |format|
if #event_sponsorship.save
format.html { redirect_to #event_sponsorship, notice: 'Event sponsorship was successfully created.' }
format.json { render json: #event_sponsorship, status: :created, location: #event_sponsorship }
else
format.html { render action: "new" }
format.json { render json: #event_sponsorship.errors, status: :unprocessable_entity }
end
end
end
Here is the form:
<%= simple_form_for(#event_sponsorship) do |f| %>
<%= f.error_notification %>
<div class="signin">
<%= f.hidden_field :event_id, value: #event %>
<%= f.hidden_field :sponsor_id, value: current_user.id %>
<%= f.button :submit, :class => "btn btn-success btn-block", value: "Yes" %>
</div>
<% end %>
In the create method the event_id should be found from the URL. Where am I going wrong?
Post your error as seen in your server console, you'll see that your param 'event_id' is a sub key of (I guess) 'event_sponsorship'
Try this:
#event = Event.find(params[:event_sponsorship][:event_id])
And it's even possible that you'll need to use this code:
#event = Event.find(params[:event_sponsorship].delete(:event_id))
That would remove the 'event_id' fron your params, and allow EventSponsorship.new to work without error
After you try to submit a new guideline and you see an error message on the form (due to the guidelines correctly failing validation)...the list of #specialties does not reload correctly (ie. it just says yes/no rather than the proper list you could see before you submitted with an error). I can't work out which part is wrong here...
VIEWS _form.html.erb
<%= simple_form_for(#guideline, html: {class: "form-horizontal"}) do |f| %>
<% if #guideline.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guideline.errors.count, "error") %> prohibited this guideline from being saved:</h2>
<ul>
<% #guideline.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :title, label: 'Title (e.g. Asthma for under 12 months)' %>
<%= f.input :specialty, as: :select, collection: #specialties %>
<%= f.input :hospital %>
<%= f.input :content, as: :string, label: 'Link' %>
<div class="form-actions">
<%= f.button :submit, :class => "btn btn-info btn-large" %>
</div>
<% end %>
guidelines_controller.rb
def new
#guideline = Guideline.new
#specialties = Guideline.order(:specialty).uniq.pluck(:specialty)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #guideline }
end
end
def create
#guideline = current_user.guidelines.new(params[:guideline])
respond_to do |format|
if #guideline.save
format.html { redirect_to #guideline, notice: 'Guideline was successfully created.' }
format.json { render json: #guideline, status: :created, location: #guideline }
else
#specialties = Guideline.order(:specialty).uniq.pluck(:specialty)
format.html { render action: "new" }
format.json { render json: #guideline.errors, status: :unprocessable_entity }
end
end
end
this is a common error. in your create action you should declare #specialties if the validation fails since that is needed in the new template.
def create
#guideline = Guideline.new params[:guideline]
if #guideline.save
else
# you need to declare #specialties here since it is needed in the new template
# which you want to render
#specialties = Specialty.all
render :new
end
end
Okay so my associations are:
Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet
My Routes:
resources :outlets do
resources :monitorings
end
View:
<%= link_to new_outlet_monitoring_path(#outlet) %>
When I click the link, the logs show that the outlet_id is passed as a parameter to the new page correctly.
But when saving the monitoring record, the outlet_id becomes nil.
Any help?
UPDATE:
# views/monitorings/_form.html.erb
<%= form_for(#monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
<div class="radio-group">
<%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
<%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
<%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
</div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>
And the controller:
# controllers/monitoring_controller.rb
def new
#monitoring = Monitoring.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #monitoring }
end
end
def create
#monitoring = Monitoring.new(params[:monitoring])
respond_to do |format|
if #monitoring.save
format.html { redirect_to #monitoring, notice: 'Monitoring was successfully created.' }
format.json { render json: #monitoring, status: :created, location: #monitoring }
else
format.html { render action: "new" }
format.json { render json: #monitoring.errors, status: :unprocessable_entity }
end
end
end
This is most likely an issue with the way you are creating the new monitoring record. Can we see your form and your create controller action?