I have the following in my view:
<%= form_for :search do |f| %>
<%= f.label :search %>
<%= f.text_field :search %>
<%= f.submit 'Search' %>
<% end %>
I am attempting to create a post action from the f.submit button which will route to a specific controller action. I then need that action to redirect back to my initial view, but pass in the text_field parameter on the redirect. How can I access the text_field parameter in my view as well as pass it through a redirect?
The easiest way to do it is from this question: Passing parameters in rails redirect_to
redirect_to search_path({ :search => params[:search] })
your parameters will come in a normal hash, and you can access them via params[:search][:search] in your controller.
generally just look into the rails server log, you can see the data your browser sends to rails right there
Related
In my Rails 6 app, I have a Product and Order model.
On my products#show page, I have some fields and a button. What I need to do is send the info to the orders#new page so that this data is shown on the orders#new page.
I have tried to write some code based on Pass variables without model associations in Rails and how to pass parameters in params using form_tag method in rails, but my code seems completely wrong.
on products#show:
<%= form_tag(new_order_path do |form| %>
<%= form.input_field :comments %>
<%= form.button %>
<% end %>
With this code I get undefined method text_field' for nil:NilClass`.
I have tried adding attr_accessor :comments to both the Product and Order model, but it doesn't help.
I don't think that my approach or what I am trying to code is right. I was just trying to piece together parts from these answers.
Can someone please help me figure out the best way I can pass this data to Orders#new to show in that view?
<%= form_with url: "/search", method: :get do |form| %>
<%= form.label :query, "Search for:" %>
<%= form.text_field :query %>
<%= form.submit "Search" %>
<% end %>
from the docs
https://guides.rubyonrails.org/form_helpers.html
and the show path also requires an id
Okay say I have a simple search form for Projects records like in the railscast found here: http://railscasts.com/episodes/37-simple-search-form?autoplay=true
Can you put the form_tag for the search in a different view other than the projects index.html? Like in a static_pages controller type of deal view? And how could you redirect to the projects index.html view after submitting the search in such a case? Thanks
Add below code wherever you want to add search functionality.
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Changes needed in your controller only. Try render view instead of redirect and you are done.
def index
#projects = Project.search(params[:search])
render `projects/index`
end
I have an Article resource and have defined resourceful routes for it. I want to create a simple page that shows the articles of the current user. I am aware that it is possible to do so by adding another action, for example 'search' to articles controller which will contain the custom code that searches for articles that have the same user id. And for the routes:
resources :articles do
get 'search'
end
But I'm not sure if adding a custom action is a good idea in this case. I'm thinking I can still use the index action (which shows all articles) and pass some sort of parameter from the url so that it can distinguish if the user wants to see all articles or just his own. But I'm not sure exactly how this can be done. Any help would be great. Thanks!
You can use the query string to pass parameters. see here
So you can pass something like .../articles?user_id=2
In your controller, just change the behavior according to the user_id parameter.
you don't need to create a new action/view for it.
You can add a small form to filter all articles or only my articles, for example:
<%= form_tag articles_path, method: :get do %>
<%= radio_button_tag :search, "all", :checked => true %>
<%= label_tag :all %><br />
<%= radio_button_tag :search, "my" %>
<%= label_tag :my_articles %><br />
<%= submit_tag "filter", name: nil %>
<% end %>
than in your controller:
def index
if params[:search] == 'my'
#articles = current_user.articles
else
#articles = Article.all
end
Hi im having two actions check and display in my controller and i have two views check.html.erb and display.html.erb in the view corresponding to those actions. The check method has a form in it's view
Here is the check.html.erb
<%= form_for :display_command_list, :method => "get", :url => {:action => "display"} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %><br />
<%= f.label :password %>
<%= f.password_field (:password) %><br />
<%= f.submit "Submit"%>
<% end%>
Below are both the actions:
def check
end
def display
#some code here
respond_to do |format|
format.html
end
end
When i fill the form in check view, it submits the form the display action and display action redirects to display view. But the problem is /display.html.erb is having all the parameters submitted in the form in it's url like this - /display?%username%=myname... I think my check method needs something to be written in it so that the form is submitted to display method and the url does not contain the parameters in the form.
I cannot use the parameters in the check method using params as they are empty and it throws a nil object error
Please help
Update:
I used a :method => post instead of a :method=> get in the form_for tag after seeing
how can I hide params I transmit to a method (like form_for seems to do)?
and it does not show any parameters in url.
But now when i go to another view from display.html.erb (say do_something.html.erb) and click on back button to come back to my display.html.erb, it says the web page expired.
Please let me know if iam not clear in asking the question or if iam doing something obviously wrong here..
Why don't you like having parameters in url? That's standard practice. Anyway you should either use POST and face some issues with browser behavior (like returning back to the page as you described or Ctrl-R-ing the page) or use GET and have all parameters in the url. That's how HTTP works.
If you have a post action to "do_something/:id" => :start (say with a named route 'start'), how do you create a form_tag that submits the :id based on a select_tag selection by the user ?
Assuming you aren't using resources.
The issue is that you have a post action, however you want to set a GET variable via a form submission(that has to use POST).
You can make the form make a GET request, like so:
<%= form_tag(start_path, :method => "get") do %>
<%= select_tag "id", "<option>1</option><option>2</option><option>3</option><option>4</option>" %>
<% end %>