How to pass parameters to a controllers in ROR - ruby-on-rails

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.

Related

How to manually set a controller variable from a Rails view

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

Rails: How to check which form in a view was submitted

There are 2 forms on one page. I want an if else statement in the controller to use different params and variable values depending on which form was submitted. From doing a google search the best I came across was to have a value on the submit button.
<%= f.submit "Save" :value => "x" %>
If this is a plausible way I cant find how to make an if else statement for checking if the submit value is 'x'.
Something like
if submit.value == 'x'
do something
else
do something else
end
Really not sure. If there is another way of having an if else statement in the controller to catch witch form was submitted by using an id or name or whatever I'm happy to hear it.
The value of the submit button can be accessed with the help of params[:commit], so you can use it to check which form is submitted.
if params[:commit] == 'x'
do something
else
do something else
end
#Pavan has the direct answer, however if you're evaluating form submissions by their respective submit values, you've got a major issue with your pattern.
Form
Forms should be a way to pass values to your controller, which will then populate the model. You should not have to determine actions based on those values, unless you have different functionality...
#app/views/posts/index.html.erb
<% #posts.each do |post| %>
<%= form_for post do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
<% end %>
The above will create multiple forms, all submitting to the posts#update method:
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def update
#post = Post.find params[:id]
#post.update post_params
end
private
def post_params
params.require(:post).permit(:x, :y, :z)
end
end
The values inside those forms don't matter, nor to which post object they were sent; they will all be evaluated in exactly the same way.
--
Actions
The other way around this is to make separate actions for the different forms:
#config/routes.rb
resources :posts do
patch :update_2, on: :member
end
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
def update_2
#post = Post.find params[:id]
#post.param = "value"
#post.update post_params
end
end
#app/views/posts/show.html.erb
<%= form_for #post, url: posts_update_2_path(#post) do |f| %>
<%= f.submit %>
<% end %>
You could use something like
<%=f.submit "Basic update", name: "basic-update" %>
<%=f.submit "Security update", name: "security-update" %>
and then check in your controller:
if params.has_key? "security-update"
#do something
elsif params.has_key? "basic-update"
#do another thing
end

Rails controller : How to pass parameters between actions in wizard style?

I'm trying to build a wizard style site to help creating an object.
Each step user inputs some parameters. I want to create and persist the object in DB only when all parameters are submitted at the final step.
class ExperimentsController < ApplicationController
def wizard_step_1
render template: "experiments/wizards/step1"
end
def wizard_step_2
render template: "experiments/wizards/step2"
end
def wizard_step_3
binding.pry
# how to access params in step 1 & 2 here?
end
end
View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
<%= text_field_tag("user_name") %>
<%= submit_tag("Next >>") %>
<% end %>
View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
<%= text_field_tag("user_email") %>
<%= submit_tag("Next >>") %>
<% end %>
I didn't put routes info here as it's tested OK. When user visit step1 and input user name, when submitting form control goes to wizard_step_2 and show view step2. Here user inputs email and when clicking submit button execution goes to action wizard_step_3.
Then my question is how to obtain user name and email the user inputs in previous steps?
I'd thought about gem Wicked, and Rails cache etc., and more or less they don't fulfill my needs. Is there a decent way doing this?
I don't have huge experience with this, but you may be able to benefit from: Wizard Forms Railscast
I'd imagine this type of setup will save the step 1, step 2, etc params as session variables:
class ExperimentsController < ApplicationController
def wizard_step_1
render template: "experiments/wizards/step1"
end
def wizard_step_2
render template: "experiments/wizards/step2"
#Create session vars from step1
session[:user][:name] = params[:user_name]
end
def wizard_step_3
binding.pry
#Step2 is already passed as params ;)
params[:user_name] = session[:user][:name]
end
end
View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
<%= text_field_tag("user_name") %>
<%= submit_tag("Next >>") %>
<% end %>
View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
<%= text_field_tag("user_email") %>
<%= submit_tag("Next >>") %>
<% end %>
I know this example is not very DRY, but it's the basis of what I'd start to look at. Essentially, you need to be able to persist data between instances of objects, which lends itself entirely to sessions

Ruby on Rails for post value of textbox

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

Passing Form Values into a controller in Rails

say I have a text field like the following in a view called 'search':
<%= text_field_tag(:lookup) %>
how do I submit this ':lookup' value and pass it into the controller called 'search' and assign it to a variable?
It's a basic problem, but being a noob, it's difficult ;)
That will be accessible in the controller as
params[:lookup]
Your controller could look something like this:
class SearchesController < ActionController::Base
def search
lookup = params[:lookup]
#models = Model.find_by_lookup(lookup)
end
end
And your view should look like this:
<%= form_tag searches_path do %>
<label for="lookup">Lookup</label>
<%= text_field_tag :lookup %>
<%= submit_tag "Submit" %>
<% end %>

Resources