In my view page I have a form like
<%= form_for (#writereview), url:
createreview_path(#writereview),:class=>"form-horizontal",method: :post do |f| %>
<div class="form-inline">
<div class="form-group">
<label for="clean">Cleanliness</label>
<%= f.select(:clean, [['1', 1], ['1.5', 2], ['2',3],['2.5',4],['3',5],['3.5',6],['4',7],['4.5',8],['5',9]],{},{ :class => "form-control"}) %>
</div>
<div class="form-group">
<label for="clean">Food</label>
<%= f.select(:food, [['1', 1], ['1.5', 2], ['2',3],['2.5',4],['3',5],['3.5',6],['4',7],['4.5',8],['5',9]],{},{ :class => "form-control"}) %>
</div>
and in controller action -
def create
#writereview = WriteReview.new(params[:writereview])
if #writereview.save!
redirect_to root_path
end
end
private
def user_params
params.require(:writereview).permit(:clean,:food,:locality,:behavior,:amenity,:likes,:dislikes,:comment)
end
Use following code:
def create
#writereview = WriteReview.new(user_params)
if #writereview.save!
redirect_to root_path
end
end
You aren't using your santization, but that isn't causing your problem.
def create
#writereview = WriteReview.new(write_params)
end
private
def write_params
params.require(:writereview).permit(:clean,:food,:locality,:behavior,:amenity,:likes,:dislikes,:comment)
end
I'd look at the model, the database schema. Your model does not know about clean, food, locality, behavior, amenity, likes, dislikes, etc. I know this because:
INSERT INTO write_reviews (created_at, updated_at) VALUES ('2015-12-06 16:04:01', '2015-12-06
There are no fields that are being recorded to the record, Your schema is not aware of these fields, so nothing is recorded.
Related
I have a has_many and belongs_to association in my Rails app. An OfficeAddress belongs_to Address, so my problem right now is how to build it on the form. When I create a new office address, to associate it into my address, the address should be created already. On my office_address_controller I have this.
class OfficeAddressesController < ResourceController
def index
#office_address = Spree::OfficeAddress.all
end
def new
new_address = Spree::Address.new
#new_office_address = new_address.office_address.build
end
def create
p params
end
end
and on my office address new.html.erb is currently empty because I can't find any documentation on how to build a form. I'd be interested in examples or documentation. Also the controller build confuses me. It didn't throw any error I was expecting error as new_address doesn't have any Id yet.
So I don't know if this is the right answer but this works for me. So here's my code.
Controller:
def new
#new_office_address = Spree::OfficeAddress.new
end
def create
b = Spree::Address.create(permit_params_address)
c = Spree::OfficeAddress.create(address_id: b.id)
if b.save && c.save
redirect_to admin_office_addresses_path
else
p b.errors.messages
end
end
View, new.html.erb
<%= form_for [:admin, #new_office_address], html: { autocomplete: "off" } do |ofc_add_form| %>
<fieldset data-hook="new_property">
<div class="row">
<div class="col-md-12">
<%= fields_for :address, Spree::Address.default do |adds|%>
<%= render :partial => 'spree/admin/shared/address_form', :locals => { :f => adds, :type => "New Office Address" } %>
<%end%>
</div>
</div>
<div class="form-actions">
<%= render partial: 'spree/admin/shared/new_resource_links' %>
</div>
</fieldset>
<% end %>
I was doing the right thing at first but I forgot to Spree::Address.default on fields_for I don't really know what it does but on the rails syntax, it accepts an object.
on my controller new method, after creating the address, to relate them with each other (OfficeAdddress) I just grab the Id from the address that was created and add it on my office address. I'm not sure this is the best way to solve the problem. I'm open for changes. I'm also adding more validation on my new method
I've been writing a new RoR app for practice. This is a basic app that is supposed to function as a lookup page for animals.
I've been working on the Create/New functions in the controller for my page. I would like to make it so that a user can enter in an animal, and have the animal save to the SQL database. Afterwards, the page should redirect to the newly created animal page.
Here's my animals_controller.rb:
class AnimalsController < ApplicationController
def index
#animals = Animal.all
end
def show
#animal = Animal.find(params[:id])
end
def new
end
def create
# render plain: params[:animal].inspect
#animal = Animal.new(animal_params)
#animal.save
redirect_to #animal
end
private def animal_params
params.require(:animal).permit(:name, :scientific_name, :range)
end
end
Here is my views/animals/new.html.erb:
<h1> Add Animal </h1>
<%= form_for :animal, url: animals_path do |f| %>
<p>
<%= f.label :name %> <br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :scientific_name %> <br>
<%= f.text_field :scientific_name %>
</p>
<p>
<%= f.label :range %> <br>
<%= f.select :range, ['land', 'sea', 'sky', 'underground'], :prompt => 'Select One' %>
</p>
<p>
<%= f.submit %>
<p>
<% end %>
When I try to enter in a new animal, here is what I get:
<ActionController::Parameters {"name"=>"cat", "scientific_name"=>"Felis catus", "range"=>"land"} permitted: false>
I'm wondering why I keep getting "permitted:false" when I have code in animals_controller.rb that states that these params are permitted! Can anyone point out anything or give me some suggestions?
Your params should look like
<ActionController::Parameters {"animal" => {"name"=>"cat", "scientific_name"=>"Felis catus", "range"=>"land"} } permitted: false>
Also, in the form, can you change :animal to #animal.
Alternatively, you can try this
params.require(:animal).permit(:name, :scientific_name, :range).permitted?
Problem is with this line render plain: params[:animal].inspect
because you are printing/accessing params directly without permission instead use :animal_params
render plain: animal_params.inspect
this lines #animal = Animal.new(animal_params) is fine. I guess your creating process works perfectly only.
I have a form in my rails app that accepts nested attributes. However, what I want to do is for rails to reject the creation of the nested model if a checkbox (outside the model itself) is checked.
Any idea on how to pass an attribute to the :reject_if option of the accepts_nested_attributes_for in the model from the controller?
Thank you very much in advance.
EDIT:
My controller looks like this:
def new
#course = Course.new
#course.course_template = CourseTemplate.new
end
def create
#course = Course.new(course_params)
#course.user = current_user
if #course.save
flash[:success] = t(".new_course_created_succefully")
redirect_to courses_path
else
render 'new'
end
end
And the form:
<%= form_for #course do |f| %>
<%= render 'shared/error_messages', error_model: #course %>
<div class="form-group has-feedback mb">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group has-feedback mb">
<div class="checkbox c-checkbox needsclick">
<label class="needsclick">
<%= check_box_tag "template", "1", false, {class: "needsclick"} %>
<span class="fa fa-check"></span>Is Template?
</label>
</div>
</div>
<%= f.fields_for :course_template do |ff| %>
<div class="form-group has-feedback mb">
<%= ff.label :name %>
<%= ff.text_field :name %>
</div>
<% end %>
<% end %>
send that checkbox as a parameter from the form and put the build operation inside an if statement. No need to bother with the reject_if
You need to handle your create and build operations separately. so instead of passing your model all attributes, youll pass the model the model attributes, and the association, the nested attributes
# controller
course = Course.new(course_params.reject{|attrib| attrib == :course_template_attributes})
unless params[:skip_create]
course.course_templates.build(course_params[:course_template_attributes]
end
...
what you need to do is conditionally create the course_templates, so you can just pass Course.new all your course_params because that creates both the course and the templates, which needs to be done separately.
Note I'm shorthanding with that reject statement up there. you can either manually add in the various params or better yet create another method with strong params and whitelist only the model attributes (not including the course_template_attributes)
additionally. the params[:skip_create] is whatever the parameter is for that checkbox that decides whether or not you want to create the templates
I'm looking to search and find results if two params exist, but i'm getting sent to car_show_path, but should have results.
Model
class Car < ActiveRecord::Base
def self.search(car_number, car_model)
where(['car_number = ? AND car_model = ?', "%#{car_number}%", "%#{car_model}%"])
end
end
Controller Show
#search = Car.search(params[:car_number], params[:car_model])
if #search.present?
#search
else
redirect_to car_path, notice: "Not a valid combination"
end
Form
<%= simple_form_for :search, url: car_show_path do |f| %>
<%= f.input :car_number, :collection => #car.collect {|c| [c.number]}, :include_blank => false %>
<%= f.input :car_model, placeholder: "Car Model" %>
<%= f.button :submit, 'Generate', class: 'btn' %>
<% end %>
You are doing it wrong. If you look into the params hash generated in the server log, you can see something like this :search => {:car_model => "value", :car_number => "Value"}. That means the values of :car_model and :car_number cannot be retrieved with params[:car_model] and params[:car_number], instead you should use params[:search][:car_model] and params[:search][:car_number]
#search = Car.search(params[:search][:car_number], params[:search][:car_model])
if #search.present?
#search
else
redirect_to car_path, notice: "Not a valid combination"
end
I see the code you have provided. I believe the code you have written need some improvement. So I have re-written code which may solve your issue and bring to your goal. Here it is :
#/app/models/car.rb
class Car < ActiveRecord::Base
def self.search(cn, cm)
where('car_number = ? AND car_model = ?', cn, cm)
end
end
#/app/controllers/cars_controller.rb
class CarsController < ApplicationController
def search
#result = Car.search params[c_number], params[c_modal]
end
end
#/app/views/cars/search.html.erb
#you can generate similar form with simple_form_for
<form action="/search" method="/get" >
<input type="text" name="c_number"
<input type="text" name="c_modal">
<input type="submit" value="search">
</form>
<% if !#result.any? %>
Not a valid combination
<% end %>
<% #result.each do |r|%>
<%= r.car_number %>
<%= r.car_modal %>
<% end %>
#/config/routes.rb
get "/search" => "cars#search"
Note : Above code is best of my practice and didn't executed locally.
Hope that helps!!!
You are putting the same values into both placeholders from the query parameter that is being sent to your search method. That doesn't seem right.
This is an excerpt from a view -
<%= #user.id %> is the user id
<%= #book.id %> is the book id
<div class="field">
<%= f.select :contribtype, options_for_select(Contribution::CONTRIB_TYPES) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I have passed the parameters for the user and the book from previous views, such that I have
http://localhost:3000/contributions/new?book_id=1&user_id=5
as the URL for the form. The correct user_id and book_id are showing up on the page.
I have the following in the controller -
def new
#user = User.find_by_id(params[:user_id])
#book = Book.find_by_id(params[:book_id])
#contribution = Contribution.new(params[book_id: #book.id, user_id: #user.id])
end
def create
#contribution = Contribution.new(contribution_params)
....
... but the user_id and book_id are not being captured in the object when it is created. I don't get any error, the data is simply not being set in the new object. Should I by passing parameters in the create action differently?
I'd use hidden fields as a quick fix:
#contribution = Contribution.new
in html:
<%= f.hidden_field :user_id, value: #user.id %>
<%= f.hidden_field :book_id, value: #book.id %>
be sure to permit those fields in your contribution_params