I'm trying to enter a URL from the view and pass it into my controller and set it equal to a variable. I'm trying it like this currently but it just keeps returning nil.
This is Controller/posts:
def run
url = params[:url_toget]
puts url
doc = Nokogiri::HTML(open(url))
post = Post.create do |post|
post.body = doc.at_css("body")
post.url_toget = url
end
end
This is Views posts:
<div class="form-group">
<%=text_field :url_toget, class:"form-control", placeholder:"Recipe URL" %>
</div>
<%= link_to "Rake", posts_run_path %>
How can I input a URL and have it pass to equal that variable?
You can't directly set variables from the view to the controller but you can post the request from the view.
Like:
<%= form_tag( '/run' ) do %>
<%= text_field_tag 'my_url' %>
<%= submit_tag %>
<% end %>
Then in your controller run action access the parameter: p params[:my_url]
Remember to enable the POST method for the run action in routes: post 'run', to: 'your_controller#run'
In my case I needed to name a form based on the Controller Action.
The way I did it was to do the following:
This is in HAML
%h2 #{params[:action].capitalize} Branch
If you wanted to do it in HTML you would do the following:
<h2>#{params[:action].capitalize} Branch</h2>
This resulted in the form being named Edit Branch when I was editing and New Branch when I was using the NEW action etc.
I hope this helps someone else.
Scott
Related
I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."
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 want to pass year and month to a controller. When the submit button is pressed, the year and month info should be passed to the action show of the controller foo.
I have edited my form as
<%= form_tag "/test" do %>
<%= month_field_tag :yearMonth %>
<%= submit_tag %>
<% end %>
How should I do to archive this in a view and how to get the passed parameter in foo controller?
Rails 4+ You must have a params.require private def in your controller to permit the variables you define as "okay" to be parsed. Your controller will filter out anything not specifically allowed via the .require.
private
def name_of_controller_params
params.require(:name_of_controller).permit(:yearMonth, :anothervar, :etc)
end
To see the params hash in your controller at the action point, you can insert this debug code into your relevant action. You can use this to see if your param is making it from the form into your controller's action.
def show
flash[:info] = "Params hash #{params}."
other actions...
end
Something like this will work:
<%= form_tag '/test' do %>
<%= month_field_tag 'month_value', '' %>
<%= submit_tag %>
<% end %>
Then inspect params from show action to see if month_value is there.
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"
Can any body tell me how should I get post value from a textbox and pass it to controller putting in if condition but no output is coming
home/index.html.erb
<%text_field_tag (:text1,nil,placeholder,"Enter the username")%>
controller/home_controller.rb
def create
#data = parmas[:text1]
if(#data=="abc")
(login to another page)
end
end
First of all put = sign before text_field_tag. You are using home controller which mostly used for static pages. To get this value you have to put this in form tag like this:
<%= form_tag root_path do %>
<%= text_field_tag (:text1,nil,placeholder,"Enter the username")%>
<%= submit_tag :search %>
<% end %>