When I'm trying to create a new "participation" in my Rails4 application and it seems like there is something wrong with my parameters. Actually this is not causing any problems in application (or I didn't notice it) but still I would like to fix it. You can see 2 "examination_id" parameters one of them is null and the other one is equal to 1.
REQUEST PARAMETERS:
{"utf8"=>"✓",
"authenticity_token"=>"XZ71eV0zxrnTBilzvEtLlHLwoAb+qKdDfxOHjrAHUPg=",
"participation"=>
{
"language_preference"=>"Türkçe",
"exam_center_preference"=>"1",
"disability"=>"1",
"user_id"=>"1",
"examination_id"=>""
},
"commit"=>"Sınava Başvur",
"examination_id"=>"1"
}
routes.rb:
resources :examinations do
resources :participations do
member do
get :update_profile_information
end
end
end
participation.rb:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
before_save :verification_key_generator
end
participations_controller.rb:
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
#examination = Examination.find(params[:examination_id])
end
def index
#participations = #examination.participations
end
def show
#participation = #examination.participations.find(params[:id])
end
def new
#participation = Participation.new
end
def create
#participation = #examination.participations.new(participation_params)
#participation.user = current_user
respond_to do |format|
if #participation.save
format.html { redirect_to [#examination, #participation], notice: 'Başvuru işlemi başarıyla tamamlandı!' }
format.json { render action: 'show', status: :created, location: [#examination, #participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: #participation.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #participation.update(participation_params)
format.html { redirect_to [#examination, #participation], notice: 'Başvurunuz Başarıyla Güncellendi!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: participation.errors, status: :unprocessable_entity }
end
end
end
private
def set_participation
#participation = Participation.find(params[:id])
end
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
app/views/participations/_form.html.erb:
<%= simple_form_for([#examination, #participation], html:{class: "well"}) do |f| %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label: 'Sınav Merkezi Seçiniz', label_method: :city %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.button :submit, "Sınava Başvur" %>
<% end %>
app/views/participations/new.html.erb:
<%= simple_form_for([#examination, #participation]) do |f| %>
<%= f.error_notification %>
<%= f.input :language_preference, collection: ["Türkçe", "İngilizce", "Rusça"], label: 'Sınav Dili Tercihi' %>
<%= f.input :exam_center_preference, collection:ExamCenter.all, label: 'Sınav Merkezi Seçiniz', label_method: :city %>
<%= f.input :disability, inline_label: 'Yardımcı İstiyorum', label: false %>
<%= f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %>
<%= f.input :examination_id, as: :hidden %>
<%= f.button :submit, "Sınava Başvur" %>
<% end %>
sa
When you're using
<%= simple_form_for([#examination, #participation], html:{class: "well"}) do |f| %>
to generate the form, it will set the action to be /examinations/[examination_id]/participations so the routes/action will know the examination_id from the url itself.
So, you don't need to pass examination_id separately as hidden field that you're setting as
<%= f.input :examination_id, as: :hidden %>
Once you remove this hidden field the request parameters will look like:
{"utf8"=>"✓",
"authenticity_token"=>"XZ71eV0zxrnTBilzvEtLlHLwoAb+qKdDfxOHjrAHUPg=",
"participation"=>
{
"language_preference"=>"Türkçe",
"exam_center_preference"=>"1",
"disability"=>"1",
"user_id"=>"1"
},
"commit"=>"Sınava Başvur",
"examination_id"=>"1"
}
Related
How can I automatically place/pass the product_id in the input field?
i have 2 tables products & capturepages in my
schema
products
t.string "name"
t.string "description"
capturepages
t.string "name"
t.string "email"
t.integer "product_id"
models
product.rb
has_many :capturepages
capturepage.rb
belongs_to :product
capturepages controller
class CapturepagesController < ApplicationController
before_action :set_capturepage, only: [:show, :edit, :update, :destroy]
def new
#capturepage = Capturepage.new
#product_id = params[:product_id]
#product = Product.find(params[:product_id])
end
def create
#capturepage = Capturepage.new(capturepage_params)
#product = #capturepage.product
respond_to do |format|
if #capturepage.save
format.html { redirect_to #product.affiliatecompanylink, notice: 'Capturepage was successfully created.' }
format.json { render :show, status: :created, location: #capturepage }
else
format.html { render :new }
format.json { render json: #capturepage.errors, status: :unprocessable_entity }
end
end
end
private
def set_capturepage
#capturepage = Capturepage.find(params[:id])
end
def capturepage_params
params.require(:capturepage).permit(:name, :email, :product_id)
end
end
views/products.show.html.erb
when the user clicks on the below link:
<%= link_to "buy now", new_capturepage_path(product_id: #product.id), target:"_blank" %>
they are directed to the capturepage form page
views/capturepages/_form.html.erb
<%= simple_form_for(#capturepage) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :product_id, value: #product_id %>
<%= f.input :name, placeholder: "Your Name", label: false %>
<%= f.input :email, placeholder: "Your Email", label: false %>
</div>
<div class="form-actions">
<%= f.button :submit, "get product" %>
</div>
<% end %>
the url states: http://localhost:3000/capturepages/new?product_id=1
capturing the product_id but the product_id input is empty:
As you are using simple_form, you should place value inside input_html. Below code should fix your problem
<%= f.input :product_id, input_html: { value: #product_id } %>
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 %>
How can I translate the following form_for - collection_select to simple_form?
<%= collection_select(:service, :service_menu_id, #service_menus, :id, :name, prompt: true) %>
To...
<%= f.input ...
Update requested:
<div class="col-xs-4">
<%= simple_form_for #service do |f| %>
<div class="field">
<!--Working - selected value saves to db-->
<%= collection_select(:service, :service_menu_id, ServiceMenu.all, :id, :name, {:prompt => true }) %>
<!--Selected value does not save to db-->
<%#= f.input :service_menu, :collection => #service_menus, label_method: :name, value_method: :id, :include_blank => false, prompt: "Select a main service" %>
<%= f.simple_fields_for :styles do |task| %>
<%= render 'style_fields', :f => task %>
<% end %>
</div>
<div class="links">
<%= link_to_add_association 'Add New Style', f, :styles, class: 'btn btn-success' %>
</div><br>
<div class="actions">
<%= f.submit :class => 'btn btn-primary'%>
</div>
<% end %>
controller
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update, :destroy]
def index
#services = current_tech.services
end
def show
end
def new
#service = current_tech.services.build
#service_menus = ServiceMenu.all
end
def create
#service = current_tech.services.build(service_params)
respond_to do |format|
if #service.save
format.html { redirect_to #service, notice: 'Service was successfully created.' }
format.json { render :show, status: :created, location: #service }
else
format.html { render :new }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
def edit
#service_menus = ServiceMenu.all
end
def update
respond_to do |format|
if #service.update(service_params)
format.html { redirect_to #service, notice: 'Service was successfully updated.' }
format.json { render :show, status: :ok, location: #service }
else
format.html { render :edit }
format.json { render json: #service.errors, status: :unprocessable_entity }
end
end
end
def destroy
#service.destroy
respond_to do |format|
format.html { redirect_to services_url, notice: 'Service was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service
#service = Service.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_params
params.require(:service).permit(:name, :service_menu_id, styles_attributes: [:id, :tech_id, :name, :hours, :minutes, :price, :details, :_destroy])
end
end
If you can wrap it as form object (i.e., f.collection_select) then this will work
<%= f.input :service_menu, :collection => #service_menus, :label_method => :name, :value_method => :id, :selected => #service.service_menu_id :include_blank => true %>
enter code here
<h1>Add New Investment Opportunity</h1>
<%= form_for Investopp.new do |f| %>
<div class="field">
<%= f.label :state_id %><br/>
<%= f.collection_select :state_id, Investopp.year_lookup(#state_ids), :label, :value, include_blank: true %>
</div>
<div class="field">
<%= f.label :city_id, "city" %><br />
<%= f.grouped_collection_select :city_id, Investopp.year_lookup(#state_ids), :cities, :value, :id,:name, include_blank: true %>
</div>
<% end %>
<%= link_to 'Back', investopps_path %>
<%= link_to 'Search', investopps_browsedisplay_path %>
class Investopp < ActiveRecord::Base
attr_accessible :Address, :Buildingname, :Desiredinvrole, :Details, :Prefferednoofinvestors, :Salesprice, :Weblisting, :photo, :user_id, :state_id, :city_id, :state, :city
has_attached_file :photo, :styles => { :small => "200x200>" }
belongs_to :user
validates :Buildingname, presence: true
validates :Address, presence: true
validates :Desiredinvrole, presence: true
validates :Weblisting, presence: true
validates :Details, presence: true
has_many :states
has_many :cities
def find_state(id)
if !id || id==0
id=1
end
#states= State.find(id)
#states.name
end
def find_city(id)
if !id || id==0
id=1
end
#cities= City.find(id)
#cities.name
end
def self.year_lookup(state_ids)
#create an emptycollection to hold the LabelValue Objects
years = []
state_ids.each do |yr| y = LabelValue.new()
y.label = yr
y.value = State.find_by_id(yr).name
years.push(y)
end
years
end
def self.state_lookup(state_ids)
years = []
state_ids.each do |yr| y = State.new()
y= State.find_by_id(yr)
years.push(y)
end
years
end
end
class LabelValue
# name the accessors. Label and Value
attr_accessor :label, :value
def cities
cityids=[]
state_cities=[]
investopps=Investopp.find(:all)
investopps.each do |i|
puts i.city_id
cityids <<i.city_id
end
cityids.uniq!
states=State.find_by_id(label)
cityids.each do |c|
if states.cities.find_by_id(c)
state_cities<< states.cities.find_by_id(c)
end
end
state_cities
end
end
class InvestoppsController < ApplicationController
# GET /investopps
# GET /investopps.json
def index
#investopps = Investopp.where(:user_id => current_user.id)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #investopps }
end
end
# GET /investopps/1
# GET /investopps/1.json
def show
#investopp = current_user.investopps.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #investopp }
end
end
# GET /investopps/new
# GET /investopps/new.json
def new
#investopp = Investopp.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #investopp }
end
end
# GET /investopps/1/edit
def edit
#investopp = Investopp.find(params[:id])
end
# POST /investopps
# POST /investopps.json
def create
#params[:investopp][:state_id]= "gopi"
#params[:investopp][:city_id]= "33"
#investopp = current_user.investopps.build(params[:investopp])
respond_to do |format|
if #investopp.save
format.html { redirect_to #investopp, notice: 'Investopp was successfully created.' }
format.json { render json: #investopp, status: :created, location: #investopp }
else
format.html { render action: "new" }
format.json { render json: #investopp.errors, status: :unprocessable_entity }
end
end
end
# PUT /investopps/1
# PUT /investopps/1.json
def update
#investopp = Investopp.find(params[:id])
respond_to do |format|
if #investopp.update_attributes(params[:investopp])
format.html { redirect_to #investopp, notice: 'Investopp was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #investopp.errors, status: :unprocessable_entity }
end
end
end
# DELETE /investopps/1
# DELETE /investopps/1.json
def destroy
#investopp = Investopp.find(params[:id])
#investopp.destroy
respond_to do |format|
format.html { redirect_to investopps_url }
format.json { head :no_content }
end
end
def lis
#state_names=[]
#state_ids=[]
#city_ids= []
#city_names=[]
#investopp = Investopp.find(:all)
#investopp.each do |item|
#state_names<< State.find_by_id(item.state_id).name
#state_ids<< item.state_id
#city_names<< City.find_by_id(item.city_id).name
#city_ids << item.city_id
end
puts #state_ids.uniq!{|i| i}
puts #city_ids.uniq!{|i| i}
puts "gopi"
respond_to do |format|
format.html { render "investopps/lis", :locals => { :state_ids => #state_ids, :city_ids => #city_ids, :investopps => #investopp } }
format.json { render json: #investopp }
end
end
end
Instead of using <%= link_to 'Search', investopps_browsedisplay_path %> you should use <%= f.submit %> and specify the action correctly. So your form view will look like this:
<h1>Add New Investment Opportunity</h1>
<%= form_for Investopp.new, :action => investopps_browsedisplay_path do |f| %>
<div class="field">
<%= f.label :state_id %><br/>
<%= f.collection_select :state_id, Investopp.year_lookup(#state_ids), :label, :value, include_blank: true %>
</div>
<div class="field">
<%= f.label :city_id, "city" %><br />
<%= f.grouped_collection_select :city_id, Investopp.year_lookup(#state_ids), :cities, :value, :id,:name, include_blank: true %>
</div>
<%= link_to 'Back', investopps_path %>
<%= f.submit 'Search' %>
<% end %>
Then you'll also need a controller method handling things as the investopps_browsedisplay_path route, which I don't see in your code anywhere. This, of course, is not a RESTful way to handle this, and there's a needed caveat that your architecture will probably confuse the problem, but so far as sending the form data, the correct way to do it is with a form submit, not a link_to, which solves the basic problem at hand.
I try to rails console for me table object but when I install sorcery or formtastic (i don't know) my record don't save.
Model
class Depositi < ActiveRecord::Base
attr_accessible :code, :description
validates_presence_of :code, :message => 'Code required!'
end
View
<%= semantic_form_for(#depositi) do |f| %>
<%= f.inputs 'Dati Deposito' do %>
<%= f.input :code, :label => 'Codice Deposito:' , :wrapper_html => { :class => 'fl' }, :required => true %>
<%= f.input :description, :label => 'Descrizione:', :wrapper_html => { :class => 'fl'}, :input_html => {:size => '15'} %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button, :label => 'Save' %>
<% end %>
<% end %>
this is my debug:
utf8 ✓
authenticity_token kVQ5OOkmGxqoZgbvteJ80M+lLHwopWEY0SI4YetQYsg=
depositi {"code":"001","description":"Test this is test"}
button
action new
Controller
def create
#depositi = Depositi.new(params[:depositi])
respond_to do |format|
if #depositi.save
format.html { redirect_to #depositi, notice: 'Record salvato con successo!' }
format.json { render json: #depositi, status: :created, location: #depositi }
else
format.html { render action: "new" }
format.json { render json: #depositi.errors, status: :unprocessable_entity }
end
end
end
This record don't save! I'm property login, i don't have error..why don't save?
RESOLVED: The problem was in my view up of <%= semantic_form_for(#depositi) do |f| %>
I write that was in conflict with formtastic render form.