I am looking to learn a few things with Ruby on Rails and was wondering how I can make a basic calculator that doesn't touch the model in rails.
I am using form_tag
This is my main page
<%= form_tag do %>
<%= label_tag('first number') %>
<%= number_field('first_number', value = nil) %>
</br>
<%= label_tag('second number') %>
<%= number_field('second_number', value = nil) %>
</br>
<%= submit_tag("calculate") %>
<% end %>
<% first_number * second_number %>
I am getting an error that says :
undefined local variable or method first_number
How would I got about fixing this? I am not sure where to go from here?
Create a controller method. Then in the form_tag give the method's url. Then receive the parameters in that method. Then calculate and show the result.
Suppose,
Your current view calculator.html.erb show the form.
in the form tag use <form action="<%= calculate_result_path %>" method="post">
Here, calculate_result is a method in any controller.
def calculate_result
#result = params[:first_number] * params [:second_number]
end
Now make a view file for this method to show the result. Or you can ajaxify to show the result on the calculator.html.erb file.
To Ajaxify
suppose, you want to show the result in calculator.html.erb as like
<div class="show-result">
<%= #result %>
</div>
now create a calculate_result.js.erb for the method calculate_result. On that JS file replace the div with class show-result using a custom div where you put your result which you get from the controller method.
To learn more about, please learn how to ajaxify your views in rails. I'll recommend you to read the book "Agile Development"
Related
im new for ruby on rails. i have 3 question
how to i translate the html code
in controllers i set
def index
#sentence = "Halo<br>worlds";
end
in index.html.erb display i set
<%= #sentence %>
but display
Halo<br>world
how do i make it like this
halo
world
how do i set onclick in submit button? i already tried set the button like this <%= submit_tag "back", onclick: "window.history.back();" %>
but the result is not go to previous page. but excetude the form..
how i can make an validation form if the validation code is in controllers. not in model
Rails automatically escapes all HTML in ERB templates. To prevent this escaping you can use the Rails ERB extension <%== %>. So in your case it would be:
<%== #sentence %>
See this question for some further discussion:
What does <%== %> do in rails erb?
Replace <%= #sentence %> with below content for a simple solution.
<% #sentence.split("<br/>").each do |word| %>
<%= word %><br/>
<% end %>
for the second qsn, check the validations in validateform() function and then respond with true or false accordingly.
<%= button_to_function "Submit", "validateform()" %>
to start out preemptively, I've already looked at various similar articles dealing with this, but I still get the error.
I'm starting out on rails and attempting to create a GPA calculator and tracker application for fun (and spent a lot of time searching through documentation); I have a singular controller and view since redirecting to an entire different page for calculating or saving a new GPA every time would look ugly.
Rails will display everything without error up until I add the form, no other erb is written currently, and the form is meant to submit letter grade values from the "f.selection" tag.
The culprit is #cgpa in <%= form_for #cgpa do |f| %>.
My form from main\index view:
<%= form_for #cgpa do |f| %>
<div class="field">
(...)
</div>
<div class="actions">
<%= f.submit 'Calculate' %>
</div>
<% end %>
My controller:
class MainController < ApplicationController
def index
##cgpa = CurrentGpa.all #currently calls a key_to error while form exists, otherwise no error is raised
#pgpa = PastGpa.all
#csem = CurrentSemester.all
#psem = PastSemester.all
end
def new
#cgpa = CurrentGpa.new
end
def create
(...)
end
end
The routes are simply Rails.application.routes.draw { root 'main#index'; resources :main }
If any other information is needed, just let me know to add >.>
When you use: <%= form_for #cgpa do |f| %> this automatically tries to submit the form to the CurrentGpasController create action and for doing so it sends a request to current_gpas_path. So you don't have this path in routes that is why it throwing error. Either you can add routes for CurrentGpa like:
resources :current_gpas
or you can specify a path in the form_for:
<%= form_for #cgpa, url: any_path do |f| %>
So this will submit your form to that url specified.
If you add the current_gpas routes then do create the controller and action to process your input.
And as mentioned in comments do add the #cgpa = CurrentGpa.new this in index action. The above will solve your error you are getting after that.
Hope this helps.
I have most of the functionality done for a site. Now I am trying to make it look nice. I have a _form.html.erb that works great.
<%= form_for(#card) do |f| %>
<% if #card.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#card.errors.count, "error") %> prohibited this card from being saved:</h2>
<ul>
<% #card.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :event %><br />
<%= f.text_field :event %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Files
view
- cards
-- new.html.erb
-- index.html.erb
-- show.html.erb
- layouts
-- application.html.erb
- pages
-- index.html.erb
I make a call for the form from new.html.erb and it works sends it to show.html.erb, just as I want. I'm using bootstrap and decided to make use of the nav bar. I have placed the nav bar code into the application.html.erb. It works just fine, well kind of. I want what would normally be a search function to be the add a new card.
When I add the form call it does not work, when I add it directly to the application page it does not work. I'm not sure, I have spent hours on this. I got it to work only on the show.html.erb page, both index pages would error out. I honestly don't remember how I did this though.
I'm trying to learn by doing, but I am stuck and need some help.
Thank you,
Ian
I guess that when you say that its working in your new.html.erb you have a new action inside your cards_controller, and inside this action you have something like: #card = Card.new
Well, if you want to put this form in another view, like in the application.html.erb you need to set first your #card variable, so you can do something like:
# application_controller:
before_filter :new_card
def new_card
#card = Card.new
end
be aware that all the controller that inherits from application controller will set this #card variable
#instance_variable
The underlying problem here is that you're calling a partial - by design, these are meant to give you the ability to call the functionality the file contains anywhere in your application
The problem you have is you're referencing an #instance_variable directly in your partial.
This isn't normally an issue - if you're using partials like you were originally (to modularize views), it should be okay. The problems arise when you try and use the partials in a more generalized way, as you are doing now:
#app/views/controller/_form.html.erb
<%= form_for(#card) do |f| %>
This relies on the #card instance variable being made available, which won't be if you're loading the partial in any other controller than the cards_controller.
--
Fix
The way to fix this is to either populate the #card instance variable in the application controller (as described by edymerchk), or to pass the raw value through the locals hash of the partial call:
This will allow you to use the card local variable in your partial:
#app/views/controller/_form.html.erb
<%= form_for card do |f| %>
-
Alternatively, you could also set the #card instance variable, as recommended in another answer:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
before_action :set_card
private
def set_card
#card = Card.new
end
end
I keep getting a undefined method `model_name' for NilClass:Class.
In the layout file: [application.html.erb]
<section id="featured">
<%= render 'subscribers/new' %>
</section>
In the form partial: [views > subscribers > _new.html.erb]
<%= form_for #subscriber, :url => subscribe_path do |f| %> [THIS LINE PRODUCES THE ERROR]
<div class="field">
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit 'Add me to the list' %>
</div>
<% end %>
In the subscribers controller: [controllers > subscribers_controller.rb]
def new
#subscriber = Subscriber.new
end
I'm a beginner at ROR, and I've looked around StackOverflow, but can't find any answers for my specific case.
What path are you hitting when you are seeing this error?
If you are navigating to any path other than /subscribers/new then #subscriber will be nil and the form will throw the error that you are seeing. You are rendering a form via a partial in your view layout, that layout is rendered (presumably) throughout the app. Thus #subsriber won't always be set.
The problem is, that you are rendering the subscribers/new template directly in the layout, but only initializing a Subscriber in the subscriber controller.
You need to create a subscribers/new.html.erb file (without the leading underscore, so it is not a partial)
Somewhere in your layout file you should have a yield call.
When you access /subscribers/new rails renders the new.html.erb template file, and stuffs it in where yield is called in the layout.
If you really need this form on every page, you will need to initialize a new subscriber on every page. You could do this with a before filter in the application controller. But then you would not need the new action in the subscriber controller.
I used this gem in my application, but I'm not sure the difference between the different implementation options for the gem:
form_for
form_tag with block
form_tag without block
Can anyone clarify? I understand that form_for is used when you wish to interact with a model, but what about the other two?
The differences are subtle, but important. form_for is a more advanced tool that yields an object you use to generate your form elements:
<% form_for(#foo) do |form| %>
<%= form.text_field(:bar) %>
<% end %>
The form_tag method is much more primitive and just emits a tag. If you want to put things inside of the <form> tag that's emitted, you put things inside the block:
<% form_tag do %>
<%= text_field_tag(:bar, 'bar_value') %>
<% end %>
Note that the form_for method handles grabbing values from your model, and will at least try to route the form to the appropriate action. With form_tag you are responsible for everything as it makes no assumptions about what you're doing.
One uses model binding and the other doesn't
As far as I know there is only one simple difference. form_tag without a block will only generate a html element for you. When you use form with a a block it will also create the form closing tag .
In example:
<% form_tag("/comments") %>
will result in
<form action="/comments">
Where
<%= form_tag("/comments") do %>
<%= submit_tag %>
<% end %>
will generate
<form action="/comments">
<input type="sumbit" />
</form>