I am trying to pass a parameter into my f.text_field but I keep receiving an error. "wrong number of arguments (3 for 1..2)"
<%= f.text_field :location, params[:location], placeholder: "Enter Zipcode", class: "form-control" %>
def edit
#city = City.find_by(id: params[:id])
end
<%= form_for(#city, :html => { :multipart => true, :class => "form-horizontal" }) do |f| %>
<%= f.text_field :location, class: "form-control", required: :required, placeholder: "Enter Zipcode" %>
<% end %>
<%= f.text_field :location, placeholder: "Enter Zipcode", value: params[:location], class: "form-control" %>
Related
I'm attempting to submit a form in Rails, and it's not creating into the db. I tried placing a binding.pry in the controller action but I'm not reaching it. Can you take a look at my form below and my controller action and let me know if I'm doing anything wrong?
<form>
<%= simple_form_for #movie do |f| %>
<div class="form-group">
<%= f.input :title, placeholder: "Movie Title", input_html: { class: 'form-control' } %>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<%= f.input :year, as: :date,
start_year: Date.today.year,
end_year: Date.today.year - 100,
discard_day: true, order: [:year],
input_html: { class: 'form-control' } %>
</div>
<div class="form-group col-md-6">
<%= f.input :genre, placeholder: "Genre", input_html: { class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<%= f.input :poster, placeholder: "Poster URL", input_html: { class: 'form-control' } %>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<%= f.input :director, placeholder: "Director",
input_html: { class: 'form-control' } %>
</div>
<div class="form-group col-md-6">
<%= f.input :rating, collection: 1..5, prompt: "1(bad) - 5(great)", input_html: { class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<%= f.association :lists, as: :radio_buttons, input_html: { class: 'form-control' } %>
</div>
<div class="form-group">
<%= f.input :plot, as: :text, placeholder: "Plot Summary", input_html: { class: 'form-control' } %>
</div>
<div class="form-group text-center">
<%= f.button :submit, "Add Movie", class: "btn btn-primary col-md-4"
%>
</div>
<% end %>
</form>
My controller actions:
def new
#movie = Movie.new
end
def create
binding.pry
#movie = Movie.new(movie_params)
if #movie.save
redirect_to movie_path(#movie)
else
flash[:danger] = "Please try again!"
redirect_to new_movie_path
end
end
def movie_params
params.require(:movie).permit(:title, :year, :genre, :poster, :director, :plot, :list_ids)
end
Any ideas here? The form will not submit.
You need to add rating and possibly lists into your movie_params
You can also clean your create method and make it simpler:
def create
#movie = Movie.new(movie_params)
if #movie.save
redirect_to #movie
else
flash[:danger] = "Please try again!"
render 'new'
end
end
You shouldn't need to use <form> when using simple_form.
I have two model:
1.Personne
class Personne < ApplicationRecord
has_one :proprietaire
accepts_nested_attributes_for :proprietaire
validates :nom, :prenom, :tel, :email,
presence: true
end
2 Proprietaire
class Proprietaire < ApplicationRecord
belongs_to :personne
validates :commune_id, :quartier,
presence: true
end
the Controller is:
class PersonneController < ApplicationController
def display_proprietaires
#proprietaires = Personne.all
##proprietaires = #proprietaires.proprietaire
end
def new_proprietaire
#provinces = Province.where(:parentId => nil)
#communes = Province.where.not(:parentId => nil)
#personne = Personne.new
#personne.build_proprietaire
end
def create_proprietaire
#proprietaire = Personne.new(proprietaire_params)
#proprietaire.build_proprietaire
respond_to do |format|
if #proprietaire.save
flash[:notice] = "succes"
flash[:type] = "success"
format.html { redirect_to action: :display_proprietaires }
else
flash[:notice] = "fail"
flash[:type] = "warning"
format.html { redirect_to action: :display_proprietaires }
end
end
end
def proprietaire_params
params.require(:personne).permit(:nom, :prenom, :tel, :email, proprietaire_attributes: [:id, :commune_id, :quartier]).except(:province, :commit)
end
end
the View is:
<%= form_for #personne, :url => url_for(:controller=>'personne', :action=>'create_proprietaire' ) do |f| %>
<div class="row">
<div class="col-xs-6 col-sm-6 col-lg-6">
<div class="form-group">
<%= f.label(:nom, 'Nom : ') %>
<%= f.text_field :nom, {class: "form-control", placeholder: 'Nom'} %>
</div>
<div class="form-group">
<%= f.label(:prenom, 'Prenom : ')%>
<%= f.text_field :prenom, {class: "form-control", placeholder: "Prenom"} %>
</div>
<div class="form-group">
<%= f.label(:tel, 'Telephone : ')%>
<%= f.text_field :tel, {class: "form-control", placeholder: "Telephone"} %>
</div>
<div class="form-group">
<%= f.label(:email, 'Email : ') %>
<%= f.text_field :email, {class: "form-control", placeholder: "Email"} %>
</div>
<div class="form-group">
<%= label_tag(:province, 'Province : ') %>
<%= select_tag(:province, options_for_select(#provinces.collect{|value| [value.denomination, value.id]}), {class: "form-control", id: "province", remote: true} ) %>
</div>
<%= f.fields_for :proprietaire do |proprio| %>
<div class="form-group">
<%= proprio.label(:commune_id, 'Commune : ') %>
<%= proprio.select :commune_id, options_for_select(#communes.collect{|value| [value.denomination, value.id]}),{}, {class: "form-control", id: "commune"} %>
</div>
<div class="form-group">
<%= proprio.label :quartier, "Quartier" %>
<%= proprio.text_field :quartier, {class: "form-control", placeholder: "Quartier"} %>
</div>
<% end %>
<%= f.submit "Enregistre", {class: 'btn btn-info'} %>
<% end %>
Routes:
resources :personne do
collection do
post :create_proprietaire
get :display_proprietaires
get :new_proprietaire
end
end
I'm new in RoR, When I try to save nothing happens, I'm getting this:
Could someone helps me on this. Thank you!
You have your association set to required but it's missing.
Associations are set to required by default in rails 5 so if you want to keep one empty you need to set optional:true on your association in model
I have been struggeling with a nested form for a while and I dont know why it is not working.
The form updates/creates the venue without complaint, but not the associated venue_image.
Problem: I want to create/update a model and one of its associated models at the same time. Please note that the controllers are namespaced under "admin"
_form.haml
.col-md-12
= form_for([:admin, venue], html: { class: 'form-horizontal', multipart: true }) do |f|
- if venue.errors.any?
.col-md-12.alert.alert-danger{role: "alert"}
%h2
= pluralize(venue.errors.count, "Error")
%ul
- venue.errors.full_messages.each do |message|
%li= message
.form-group
= f.label :name, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :name, class: 'form-control'
.form-group
= f.label :category, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :category, class: 'form-control'
.form-group
= f.label :description, class: 'col-md-2 control-label'
.col-md-10
= f.text_area :description, rows: 5, class: 'form-control'
.form-group
= f.label :street, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :street, class: 'form-control'
.form-group
= f.label :zip, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :zip, class: 'form-control'
.form-group
= f.label :city, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :city, class: 'form-control'
.form-group
= f.label :homepage, class: 'col-md-2 control-label'
.col-md-10
= f.url_field :homepage, class: 'form-control'
%h3 Add images
= f.fields_for(:venue_image, html: { class: 'form-horizontal', multipart: true }) do |vi|
.form-group
= vi.label :name, class: 'col-md-2 control-label'
.col-md-10
= vi.input :name, label: false, class: 'form-control'
.form-group
= vi.label :venue_id, class: 'col-md-2 control-label'
.col-md-10
= vi.input :venue_id, label: false, class: 'form-control'
.form-group
= vi.label :default, class: 'col-md-2 control-label'
.col-md-10
= vi.input :default, as: :radio_buttons, label: false, class: 'form-control radio radio-inline'
.form-group
= vi.label :image_file, class: 'col-md-2 control-label'
.col-md-10
= vi.file_field :image_file, label: false, class: 'form-control'
.actions
= f.submit 'Save', class: 'btn btn-primary pull-right'
= link_to 'Cancel', :back, class: 'btn btn-default'
venues_controller
class Admin::VenuesController < Admin::BaseController
before_action :set_venue, only: [:show, :edit, :update, :destroy]
def index
#venues = Venue.all
end
def show
end
def new
#venue = Venue.new
#venue.venue_images.build
end
def edit
end
def create
#venue = Venue.new(venue_params)
if #venue.save
redirect_to admin_venue_path(#venue), notice: 'Venue was successfully created.'
else
render :new
end
end
def update
if #venue.update(venue_params)
redirect_to admin_venue_path(#venue), notice: 'Venue was successfully updated.'
else
render edit_admin_venue
end
end
def destroy
#venue.destroy
redirect_to admin_venues_url, notice: 'Venue was successfully destroyed.'
end
private
def set_venue
#venue = Venue.find(params[:id])
end
def venue_params
params.require(:venue).permit(:name, :category, :description, :street, :zip, :city, :homepage,
venue_image_attributes: [:name, :default, :image_file])
end
end
venue_images_controller
class Admin::VenueImagesController < Admin::BaseController
def new
image = VenueImage.new
render locals: { image: image }
end
def create
# TODO: Remove # if possible
#image = VenueImage.new(venue_images_params)
if #image.save
redirect_to admin_venue_path(#image.venue.id), notice: 'Image was successfully created.'
else
render admin_new_venue_path
end
end
private
def venue_images_params
params.require(:venue_image).permit(:name, :default, :image_file, :venue_id)
end
end
routes
namespace :admin do
resources :venues do
resources :venue_images
end
resources :users
end
Thanks in advance for any help! Please let me know if you need more of the code.
Seems like you have a has_many relation with the Venue(i.e, has_many :venue_images), then this line
= f.fields_for(:venue_image, html: { class: 'form-horizontal', multipart: true }) do |vi|
should be
= f.fields_for(:venue_images, html: { class: 'form-horizontal', multipart: true }) do |vi|
And your venue_params method should be like below
def venue_params
params.require(:venue).permit(:id, :name, :category, :description, :street, :zip, :city, :homepage, venue_images_attributes: [:id, :name, :default, :image_file])
end
Notice the plural venue_images and also I added :id to venue_params for Update to work correctly.
I have a simple forms which is working well in a sense that it shows the validation error:
<%= simple_form_for( #my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.text_field :title, :placeholder => t("title") %>
</p>
......
but it just isn't showing which of the fields are invalid. How can I make it do that?
You should use it this way:
<%= simple_form_for( #my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.input :title, :placeholder => t("title") %>
</p>
......
use input instead of text_field.
Check examples in documentation here.
Hope that helps!
<%= simple_form_for( #my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs <% unless #my_model.errors[:title].empty? %>error<% end %>">
<p>
<%= f.input_field title, :placeholder => t("title") %>
<%= f.full_error :title %>
</p>
......
My view code is
<%= form_for(#payment,:url => verify_payment_payment_index_path) do |f| %>
<%= f.radio_button("amount", "10") %>
<%= label :amount, '10 Rs',:style => "display:inline" %> <br>
<%= f.radio_button("amount", "20") %>
<%= label :amount, '20 rs',:style => "display:inline" %><br>
<%= f.radio_button("amount", "50") %>
<%= label :amount, '50 rs',:style => "display:inline" %><br>
<%= f.radio_button :amount, '100' %>
<%= label :amount, '100 rs',:style => "display:inline" %><br>
<%= link_to "Make Payment", verify_payment_payment_index_url,
:class => "btn",
:data => { :toggle => "modal",
"target" => "#myBox" },
"for"=>"Make Payment" %>
<% end %>
And I want to access all values in params in controller action after click on link "Make Payment". So how can I achieve it in rails 3?. Please note that I have to open lightbox on click of link and then submit form
you need to submit the form!
so try the following:
<%= form_for(#payment,:url => verify_payment_payment_index_path) do |f| %>
<%= f.radio_button("amount", "10") %>
<%= label :amount, '10 Rs',:style => "display:inline" %> <br>
<%= f.radio_button("amount", "20") %>
<%= label :amount, '20 rs',:style => "display:inline" %><br>
<%= f.radio_button("amount", "50") %>
<%= label :amount, '50 rs',:style => "display:inline" %><br>
<%= f.radio_button :amount, '100' %>
<%= label :amount, '100 rs',:style => "display:inline" %><br>
<%= f.submit "Make Payment", :class=>"btn",:data => {:toggle => "modal","target"=>"#myBox"},"for"=>"Make Payment" %>
<% end %>