Undefined method - passing a value from one view to another - ruby-on-rails

I have a form in a view similar to this:
<%= form_tag("mystuff/new", method: "get") do %>
<% #accesses.each do |access| %>
<%= radio_button_tag(:cost, access.cost) %>
<%= label_tag :label,access.label, :class=>"control-label" %>
<%= label_tag :costlabel, access.cost, :class=>"control-label" %> <%= label_tag :costlabel2, "Euros", :class=>"control-label" %>
<% end %>
<%= submit_tag "Go" %>
<% end %>
My controller has:
helper_method :amount
def new
#amount = params[:cost]
end
and the view of mystuff/new has a line like:
<div class="row-fluid offset1">Stuff: <%= amount -%> </div>
I get Error undefined method `amount'. What I want to do is pass the value of the radio button to the next view, but I don't want to use a database. What's the proper way to do that in rails?

You are missing a # here. #amount.

Related

undefined local variable or method `f' when using hidden_field tag

I'm following a tutorial, and i get this error when i try to add f.hidden_field tag in my form.
undefined local variable or method 'f'
<h1>Signing up for "<%= #subscription.plan.name %>"</h1>
<%= f.hidden_field :paypal_customer_token %>
<%= f.hidden_field :paypal_payment_token %>
<% if #subscription.paypal_payment_token.present? %>
<H1>Payment has been provided </H1>
<% else %>
<div class="field">
<%= radio_button_tag :pay_with, :paypal %>
<%= label_tag :pay_with_paypal do %>
<%= image_tag "paypal.png" %>
<% end %>
<%= link_to "paypal.png", paypal_checkout_path(:plan_id => #subscription.plan_id) %>
<% end %>
Either that tutorial is broken or you missed the line where they do something like this:
<%= form_for #subscription do |f| %>

Rails 5: Nil error in the first argument of a form

I'm quite new with rails so excuse my ignorance.
I have an homepage with a view of astronomic observations and I want to put in the same page the view of the outings that the observatory organize.
Since I can't append two views in the same page I tryed to pass the second view as a partial but I get an error
First argument in form cannot contain nil or be empty
So I though it was a problem of variables and tryed to pass the local variable of the partial but nothing changed.
So how do I pass a variable of #outing to the partial outings/_index.html.erb which will be visualized in the view of observative_sessions
I defined #outing=Outing.new in outings_controller which is the controller for the view outings
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render 'outings/index', :outings => #outing %>
In app > views > outings I put an _index.html.erb
<%= render 'outings/new_modal' %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render 'outings/form' %>
And then the form
<%= bootstrap_form_for #outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
I get the error in the first line of the form with #outing.
If I put the view in a page alone everything work but not with the partial.
So what did I do wrong?
Thank you in advance for all the help.
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render partial: 'outings/index', locals: {outing: #outing} %>
In app > views > outings I put an _index.html.erb
<%= render partial: 'outings/new_modal',locals: {outing: outing} %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render partial: 'outings/form', locals: {outing: outing} %>
Then in your form use the local outing variable:
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
You need to pass #outing variable to all forms those are using #outing variable.
Change the line of rendering _new_modal.html.erb partial like this
<%= render 'outings/new_modal', outing: outings %>
Then change the line of rendering _form.html.erb like this
<%= render 'outings/form', outing: outing %>
And finally change the actual form like this
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>

How do i route a particular controller action to a particular form in rails?

Hi I am new to RoR and I was making a simple math_app. The addition function works fine. Now I'm trying to do a simple subtraction.The subtract controller is invoked and the subtract form is displayed but when I click on subtract the add controller gets invoked and an addition is performed. Where have I gone wrong?
This is my routes.rb:
Rails.application.routes.draw do
get 'subtract/form'
post 'subtract/result'
get 'add/form'
post 'add/result'
end
result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>
form.html.erb: (This is the subtraction form)
<%= form_tag subtract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
subtract_controller.rb:
class SubtractController < ApplicationController
def form
end
def result
#first = params[:first].to_i
#second = params[:second].to_i
#result = #first - #second
end
end
You have problem in views/substract/form.html.erb. It now reads:
<%= form_tag add_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
But it should be:
<%= form_tag substract_result_path do %>
<%= number_field_tag :first %>
-
<%= number_field_tag :second %>
<%= submit_tag "subtract" %>
<% end %>
You should probably also fix route
post '/subtract/result' => 'subtract#result', as: 'substract_result'
Also in views/substract/result.html.erb:
<%= #first %> - <%= #second %> = <%= #result %>
<br/>
<%= link_to 'back', subtract_form_path %>

Posting a variable to another controller

I have a welcome controller/view.
On the index.html.erb I have a simple form that takes in one value:
<%= form_tag do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Upon hitting the submit button I'd like to pass the zip variable to another controller called "theaters". The variable doesnt need to be saved in any kind of model, its just being used to execute an API call in the Theaters controller.
Whats the simplest way to do this?
Thanks
Try this:
<%= form_tag({controller: "theaters", action: "index"}) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Hope it helps.
Make a route for your method in TheatresController, lets assume your method name is get_zip
post '/get_zip' => 'theaters#get_zip', as: "zip"
Create your form
<%= form_tag(url: zip_path, method: :post) do %>
<div>
<%= label_tag(:zip, "Enter Zipcode to search in:") %>
<%= text_field_tag(:zip) %>
</div>
<%= submit_tag("Search") %>
<% end %>
Access it inside your method:
def get_zip
#zip = params[:zip]
#your logic
end

Rails: Iterate through attributes with simple_form

I have a rails model with a large number of fields (112), for loading a configuration. I would like, in the edit and show forms, display only if the field is already filled. That is, if the field is null in the database then do not display it for edit.
There are two records - the main one is Batch and there is a 1:1 relationship to the Primer3Batch class. I'm trying to do an edit on the Primer3Batch class on the show action of Batch, I'm not sure if this is a good idea or will even work.
I have tried using the attributes method and am getting this error:
undefined method `attributes' for #<SimpleForm::FormBuilder
batches_controller.rb
def show
#batch = Batch.find(params[:id])
#primer3 = Primer3Batch.where(:batch_id => #batch.id)[0]
respond_to do |format|
format.html # show.html.erb
format.json { render json: #batch }
end
end
batches/show.html.erb
<h1>Batch Details: <%= #batch.id %></h1>
<%= simple_form_for(#primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% f.attributes.each_attribute do |a| %>
<% if a %><%# if a is not nil %>
<%= f.input a %><%# send the field to the form %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
EDIT
Thank you JSWorld for pointing out the error of using the instance variable. I have corrected it and seem to have gotten further but it's still not quite right. This is the line changed - note attributes.each as attributes.each_attribute doesn't work.
<% #primer3.attributes.each do |a| %>
Now I am getting an error on the form fields:
undefined method `["id", 110]' for #<Primer3Batch:
I guess I need to somehow turn this :
a ["id", 110]
into:
<%= f.input :id %>
* EDIT 2 *
Final code block based on IIya Khokhryakov's answer.
<%= simple_form_for(#primer3) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<% #primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I hope your intention is #primer3.attributes and not f.attributes. The error is because f is the form object and does not have attributes associated with it.
#primer3.attributes is a Hash of the model's attributes and their values. So you can do something like this:
<% #primer3.attributes.each_pair do |name, value| %>
<%= f.input name if value %>
<% end %>

Resources