How to get params value in action on link click - ruby-on-rails

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 %>

Related

How do I retrieve a parameter into a f.text_field?

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" %>

Using fields_for in Rails 4...does not save the new fields

I am trying to use the fields_for helper method on a project I am working on. The original form works and saves just fine. The new attributes do not save and I get a NoMethodError and a undefined method. What am I missing?!
Here is my listing model:
class Listing < ActiveRecord::Base
has_one :listing_commerical_attribute
accepts_nested_attributes_for :listing_commerical_attribute, :allow_destroy => true
Here is my listing_commercial_attribute model:
class ListingCommercialAttribute < ActiveRecord::Base
belongs_to :listing
accepts_nested_attributes_for :listing
end
Here is my controller:
def new
#listing.build_listing_commercial_attribute
respond_to do |format|
format.html # new.html.erb
format.json { render json: #listing }
end
end
private
def commercial_params
params.require(:commerical_listing_attribute)
.permit(:gas_pipe_size,
:amperage,
:basement_ceiling_height,
:ceiling_height,
:door_size,
:zoning,
:previous_use,
:community_board,
:delivery_date,
:key_money,
:security_deposit,
:price_per_sq_ft,
:did_size)
end
Here is my _form.html.erb:
<h2 class="text-center">Commercial</h2>
<%= f.fields_for :listing_commerical_attributes do |ff| %>
<div class="field">
<%= ff.label :gas_pipe_size, "Gas Pipe Size", class: "general-text-label" %>
<%= ff.number_field :gas_pipe_size, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :amperage, "Amperage", class: "general-text-label" %>
<%= ff.number_field :amperage, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :ceiling_height, "Ceiling Height", class: "general-text-label" %>
<%= ff.number_field :ceiling_height, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :basement_ceiling_height, "Basement Ceiling Height", class: "general-text-label" %>
<%= ff.number_field :basement_ceiling_height, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :door_size, "Door Size", class: "general-text-label" %>
<%= ff.number_field :door_size, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :zoning, "Zoning", class: "general-text-label" %>
<%= ff.text_field :zoning, class: "general-text-field" %>
</div>
<div class="field">
<label for="tenant_improvements" class="general-text-label">Tenant Improvements <small>(If Applicable)</small></label>
<%= ff.text_area :tenant_improvements, :rows => "4", class: "general-text-area" %>
</div>
<div class="field">
<label for="previous_use" class="general-text-label">Previous Use <small>(If Applicable)</small></label>
<%= ff.text_area :previous_use, :rows => "4", class: "general-text-area" %>
</div>
<div class= "field">
<%= ff.label :community_board, "Community Board", class: "general-text-label" %>
<%= ff.text_field :community_board, class: "general-text-field" %>
</div>
<div class="field">
<%= ff.label :delivery_date, "Delivery Date", class: "general-text-label" %>
<div class="input-group">
<span class="input-group-addon"><i class="nklyn-icon-calendar"></i></span>
<%= ff.text_field :delivery_date, :class => "datepicker general-text-field" %>
</div>
<div class="field">
<%= ff.label :key_money, "Key Money", class: "general-text-label" %>
<div class="input-group">
<span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span>
<%= f.text_field :key_money, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %>
</div>
</div>
<div class="field">
<%= ff.label :security_deposit, "Security Deposit", class: "general-text-label" %>
<div class="input-group">
<span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span>
<%= f.text_field :security_deposit, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %>
</div>
</div>
<div class="field">
<%= ff.label :price_per_sq_ft, "Price Per Sq Ft", class: "general-text-label" %>
<div class="input-group">
<span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span>
<%= f.text_field :price_per_sq_ft, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %>
</div>
</div>
<div class="field">
<%= ff.label :did_size, "Drive In Doors Size", class: "general-text-label" %>
<%= ff.number_field :did_size, class: "general-text-field" %>
</div>
<% end %>
Update
I made the change to the ListingCommercialAttribute model and removed the accepts nested attributes for.
I changed the f.fields_for to singular instead of plural.
I added in the nested attributes after the parent (see below)
def listing_params
params.require(:listing)
.permit(:access,
:address,
:apartment,
:cats_ok,
:cross_streets,
:dogs_ok,
:latitude,
:longitude,
:amenities,
:date_available,
:bathrooms,
:bedrooms,
:description,
:fee,
:exclusive,
:featured,
:rental,
:residential,
:landlord_contact,
:listing_agent_id,
:sales_agent_id,
:neighborhood_id,
:pets,
:photo,
:photo_tag,
:primaryphoto,
:price,
:square_feet,
:station,
:status,
:subway_line,
:term,
:title,
:utilities,
:move_in_cost,
:owner_pays,
:private,
:office_id,
:full_address,
:zip,
:convertible,
:landlord_llc,
:pinned,
:image,
listing_commercial_attribute_attributes: [
:gas_pipe_size,
:amperage,
:basement_ceiling_height,
:ceiling_height,
:door_size,
:zoning,
:previous_use,
:community_board,
:delivery_date,
:key_money,
:security_deposit,
:price_per_sq_ft,
:did_size])
end
Here are my new controller actions:
def edit
#listing.attributes = listing_params
end
def create
#listing.attributes = listing_params
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render json: #listing, status: :created, location: #listing }
else
format.html { render action: "new", notice: "Correct the mistakes below to create the new listing" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
But now I am getting a NoMethodError in Listings#show error. I created a partial for the commercial attributes. Shouldn't they be included now that they are in the strong params, or am I totally misunderstanding that?!
Here is the partial:
Gas Pipe Size: <%= listing_commercial_attributes.gas_pipe_size(#listing) %>
Amperage: <%= listing_commercial_attribute.amperage(#listing) %>
Basement Ceiling Height: <%= listing_commercial_attribute.basement_celing_height(#listing) %>
Ceiling Height: <%= listing_commercial_attribute.ceiling_height(#listing) %>
Door Size: <%= listing_commercial_attribute.door_size(#listing) %>
Zoning: <%= listing_commercial_attribute.zoning(#listing) %>
Build to Suit: <%= listing_commercial_attribute.build_to_suit(#listing) %>
Previous Use: <%= listing_commercial_attribute.previous_use(#listing) %>
Community Board: <%= listing_commercial_attribute.community_board(#listing) %>
Delivery Date: <%= listing_commercial_attribute.delivery_date(#listing) %>
Key Money: <%= listing_commercial_attribute.key_money(#listing) %>
Update #2
I changed it to singular.
Here is the complete error.
NameError in Listings#show
Showing /Users/Code/app/views/listings/_commercial_attributes.html.erb where line #1 raised:
undefined local variable or method `listing_commercial_attribute' for #<#:0x007f86606f6a10>
Did you mean? listing_collection_url
Gas Pipe Size: <%= listing_commercial_attribute.gas_pipe_size(#listing) %>
Amperage: <%= listing_commercial_attribute.amperage(#listing) %>
Basement Ceiling Height: <%= listing_commercial_attribute.basement_celing_height(#listing) %>
Ceiling Height: <%= listing_commercial_attribute.ceiling_height(#listing) %>
Door Size: <%= listing_commercial_attribute.door_size(#listing) %>
Zoning: <%= listing_commercial_attribute.zoning(#listing) %>
Trace of template inclusion: app/views/listings/_listing_content_area.html.erb, app/views/listings/show.html.erb
Update #3
def show
#my_listing_collections = ListingCollection.with_agent(current_agent).order("created_at DESC")
#listing_commercial_attributes = ListingCommercialAttribute.find(params[:id])
#regions = Region.order(name: :asc)
#listing = Listing.includes(:photos, :likes, :interested_agents).find(params[:id])
if #listing.private && cannot?(:create, Listing)
redirect_to listings_path, notice: 'This listing is no longer available'
else
agent = Agent.where(id: params[:agent_id]).first
#page = Listings::ShowView.new(#listing, agent)
respond_to do |format|
format.html
end
end
end
I keep getting this error:
ActiveRecord::RecordNotFound in ListingsController#show
Couldn't find ListingCommercialAttribute with 'id'=5755
It is searching for the commercial attribute with an id of 5755, but that is the listing id. I'm not sure what to pass in there...
Do not define accepts_nested_attributes_for on both models. Only on the parent model. Otherwise you'll run into circular dependency issues. In this case the parent model looks like it's a Listing, so remove accepts_nested_attributes_for :listing from ListingCommercialAttribute.
The first argument to f.fields_for should be the name of the association and yours is slightly off. You have has_one : listing_commerical_attribute so you want f.fields_for : listing_commerical_attribute.
The Strong Parameters should require your parent object first and include nested objects second. Also, you must append _attributes to the end of your nested attribute name.
So, for 3:
def listing_params
params.require(:listing)
.permit(:id,
# ...
listing_commercial_attribute_attributes: [ # Note: _attributes
:gas_pipe_size,
# ...
])
end
In the create/edit actions, be sure to set the params from the strong parameters method: #listing.attributes = listing_params.
Read more in the docs on accepts_nested_attributes_for and Strong Parameters.

How to write a rspec for update action in view?

Only User with attribute curator equal true can update homepagelist in article to true and it works fine .
<% if #article.homepagelist%>
<% if current_user.curator %>
<%= form_for #article do|f| %>
<%= f.hidden_field :article_id, :value => #article.id%>
<%= f.hidden_field :title, :value => #article.title%>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :body, :value => #article.body%>
<%= f.hidden_field :image, :value => #article.image%>
<%= f.hidden_field :plain_body, :value => #article.plain_body %>
<%= f.hidden_field :magazine_id, :value => #article.magazine_id%>
<%= f.hidden_field :is_sponsored, :value => #article.is_sponsored%>
<%= f.hidden_field :ad_title, :value => #article.ad_title %>
<%= f.hidden_field :homepagelist, :value => false %>
<%= f.submit "Delete from List" %>
<% end %>
<% end %>
<% else %>
<div class="col-md-2"></div>
<% if current_user.curator %>
<%= form_for #article do|f| %>
<%= f.hidden_field :article_id, :value => #article.id%>
<%= f.hidden_field :title, :value => #article.title%>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :body, :value => #article.body%>
<%= f.hidden_field :image, :value => #article.image%>
<%= f.hidden_field :plain_body, :value => #article.plain_body %>
<%= f.hidden_field :magazine_id, :value => #article.magazine_id%>
<%= f.hidden_field :is_sponsored, :value => #article.is_sponsored%>
<%= f.hidden_field :ad_title, :value => #article.ad_title %>
<%= f.hidden_field :homepagelist, :value => true %>
<%= f.submit "Add List" %>
<% end %>
<% end %>
<% end %>
I want to test the update action after i click on add to list or remove from list that change only the homepagelist variable in article to true when i click add to list and to false when i click on rmove from list
describe 'POST #update/homepagelistvariable' do
it 'allows curator to update homepagelist' do
#user = create(:user)
sign_in #user
#user.curator = true
#article2 = create(:article)
#article2.homepagelist = true
patch :update, article: FactoryGirl.attributes_for(:article,
homepagelist:
#article2.homepagelist)
end
end
describe 'PUT #update/homepagelist' do
it 'curator can add article to homepagelist' do
#user = create(:user, :curator => true)
sign_in #user
#article = create(:article, :user_id => #user.id)
put :update, id: #article.id,
article: FactoryGirl.attributes_for(:article,
homepagelist:
'true')
#article.reload
expect(assigns(:article).homepagelist).to match(true)
end
end
it 'curator can remove article to homepagelist' do
#user = create(:user, :curator => true)
sign_in #user
#article = create(:article, :user_id => #user.id,
:homepagelist => true )
put :update, id: #article.id,
article: FactoryGirl.attributes_for(:article,
homepagelist:
'false')
#article.reload
expect(assigns(:article).homepagelist).to match(false)
end
end

Simple_form_for - show validation errors for each field

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>
......

div class wrapping fieldwitherrors won't work

My Rails application just wont add any class to fields with errors. Cant find wihat is the problem.
Got this in model:
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :phone
Any ideas where to start looking for solutions ?
This is the view erb file which does not generate the required styled class:
<%= form_for :company, :url => {:action => 'create_lead'}, :html => {:class => "form-horizontal"} do |f| %>
<div class="">
<div class="span2">
<%= f.label :csdd_nr, "CSDD numurs" %>
<%= f.text_field :csdd_nr, {:class => "input-small"} %>
</div>
<div class="span4">
<%= f.label :name, "Nosaukums" %>
<%= f.text_field :name %>
</div>
<div class="span6">
<%= f.label :ap_veh_count, "Auto skaits" %>
<%= f.text_field :ap_veh_count, {:class => "input-small"} %><br /><br />
</div>
<div class="span6">
<%= f.label :office_adress_street, "Faktiskā adrese" %>
<%= f.text_field(:office_adress_street, {:placeholder => 'Iela', :class => "input-medium"}) %> <%= f.text_field(:office_adress_city, {:placeholder => 'Pilsēta', :class => "input-small"}) %> <%= f.text_field(:office_adress_postcode, {:placeholder => 'Pasta indekss', :class => "input-small"}) %>
</div>
<div class="span4">
<%= f.label :web, "Mājaslapa" %>
<%= f.text_field :web %><br /><br />
</div>
<div class="span4">
<%= f.label :phone, "Telefona numurs" %>
<%= f.text_field :phone %>
</div>
<div class="span4">
<%= f.label :email, "E-pasts" %>
<%= f.text_field :email %>
</div>
<div class="span4">
<%= f.label :company_field, "Uzņēmuma nodarbošanās" %>
<%= f.text_field :company_field %><br /><br />
</div>
<%= f.hidden_field(:company_status, :value => "3") %>
<div class="span12">
<br /><br />
<%= submit_tag("Saglabāt", :class => 'btn btn-primary') %>
<%= link_to "Atcelt", {:action => 'list_leads'}, :class => 'btn' %>
</div> def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
</div>
<% end %>
OK, and here is the controller which saves the data to database:
def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
This happens because you're redirecting instead of rendering, when there is a validation error. Your controller should look like:
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
render(:action => 'new_lead')
end
end

Resources