I feel like there's no easy way to do this in rails, but since I'm fairly noob in rails I decided to ask for solutions:
I have a form in a view that contains a single (text) input. How can I specify the form url such that it will do a GET to /something/<input> ?
I know I could:
use custom javascript code
post to an endpoint that would do the redirect
Is there any cleaner way?
(using rails 5.2.1 if relevant)
I think you could use something like this:
<%= form_tag(route_path method: :get) do %>
search <%= text_field_tag :search %>
<%= submit_tag 'Search' %>
<% end %>
It depends on which form helpers you use.
Maybe you can try this
In your form:
<%= form_with(url: "path_name/input_field_name", method: :get, local: true) do |f| %>
<%= f.text_field :input_field_name %>
<%= f.submit 'Search', input_field_name: nil %>
<% end %>
In your controller:
unless params[:input_field_name].blank?
#results = ModelName.where('input_field_name iLIKE ?', "%#{params[:input_field_name]}%")
end
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
I have two engines in refinerycms: product and order. Im trying to call "create" method of orders while being on the product show page, but cant get the right syntax.
The html is:
<%= form_tag({controller: 'orders', action: 'create'}, method: 'post') do %>
<%= submit_tag('Submit') %>
<% end %>
And the error is :
No route matches {:action=>"create", :controller=>"refinery/products/orders", :id=>"category1", :locale=>:en}
The route I`m looking for is(from rake routes log):
orders_orders POST /orders(.:format) refinery/orders/orders#create
I`ve tried different variants, like:
<%= form_tag(url_for({controller: 'orders', action: 'create'}), method: 'post') do %>
<%= submit_tag('Submit') %>
<% end %>
<%= form_tag({controller: 'refinery/orders/', action: 'create'}, method: 'post') do %>
<%= submit_tag('Submit') %>
<% end %>
And some other. But had no luck.
How can I call 'create' method of orders from products/show page and transfer there correct params?
Well, I have used a kind of workaround to accomplish my goal. Do not know if this is a ruby-way, but still:
1. I have created a new action in my products controller.
def ordering
#Call any method you like.
end
I have added this in routes:
post 'shop/products/ordering' => 'refinery/products/products#ordering'
Still this is not a good solution, because there is no way to call actually another controller`s method without directly creating an instance of this controller or marking the method as a class method, not instance.
In any case, I`d be very grateful if someone could explain in details or show any documentation on how to do such things directly.
UDP: I`ve found a good way in search engine for this cms.
<%= form_tag refinery.search_root_path, method: 'get' do %>
<%= label_tag 'query', t('.search_label') %>
<%= text_field_tag :query, {}, {type: "search", placeholder: t(".search_site_for"), value: (params[:query] if params[:query])} %>
<%= submit_tag t(".go") %>
<% end %>
Here's my form:
<%= form_for #asset do |f| %>
<%= f.check_box :remove_picture %>
<%= f.submit "Remove" %>
<% end %>
How could I just make this one button that does :remove_picture and submit? Thanks
Check out the API dock for the form_for helper:
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for
You can force the form to use the full array of HTTP verbs by setting
:method => (:get|:post|:put|:delete)
So your code might look like
<%= form_for(#asset, html: { method: :delete }) do |f| %>
<%= f.submit "Remove" %>
<% end %>
You could change the checkbox to a hidden field on the form...
If it were me, I'd look at something like button_to and handle this via AJAX on the controller. This way the button would call a controller action, say remove_picture and return a JS response which could update your view.
Example:
button_to([remove_picture, #asset], { method: :delete })
Note: method: :delete may not be needed - depends on your routes.
I am stuck one more time ... and one more time I suspect it's a stupid syntax problem:
I want to pass 2 vaiables in the url with my super simple search form.
I was expecting a URL like this:
http://mydomain/categories/search?search=pdf&os=2
But I get this:
http://mydomain/categories/search?search=pdf&os[]=
I thought it should work like this:
<% form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= hidden_field :os, params[#category.id] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
... but well, it didn't do it ...
Does anyone know where I am going wrong?
Thanks!
Val
You need to modify the line a bit, using hidden_field_tag:
<%= hidden_field_tag :os, :value => #category.id %>
See the hidden_field_tag documentation for more information.
<%= hidden_field :os, params[#category.id] %>
Is going to access a key in the params hash with #category.id, is there such a key? Looks like not, as its returning nil.
Seems like you want something to the effect of
<%= hidden_field :os, #category.id %>
One of the things I'm doing includes several links on the show view. For instance, I have a link (or button) for "Accepting", and another one for "Rejecting". Click on Accept, and the model updates the is_accepted field as true, click on Reject, and the is_accepted field is false.
Now, how best do I handle this? In ASP.NET, I would have simply created a LinkButton and written a handler, but Rails doesn't work that way, so I'm trying to figure out how to essentially replicate what a LinkButton would do.
Right now, I'm coding two forms on the same view, nearly identical, that look like this:
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '1' %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '0' %>
<%= f.submit "Reject" %>
<% end %>
This feels weird to me, but I can't seem to find anything that says this is the wrong way to do it.
I could, I assume, dry things up by using a partial and/or a helper method, but I wanted to make sure I'm on the right track and not doing something totally wrongly.
You can give your submit tag a name.. ie
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]' %>
<%= f.submit "Accept", :name => 'accept' %>
<%= f.submit "Reject", :name => 'reject' %>
<% end %>
Then you can detect the name in params[] and skip the '1'/'0' value.
I think you're going about it the right way. One way to clean up your forms is by using the model form helpers all the way through, so you'd end up with something like
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => true %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => false %>
<%= f.submit "Reject" %>
<% end %>
But other than that, it looks like the right way to go about it. I would suggest against creating new methods to do this, because you're not doing anything outside of normal web requests (updating a model in this instance).
Using the submit tag as the switch and detecting it in params[] is also a good way, but I usually prefer to keep my controllers as vanilla as possible. In the end, both of these ways would end up with the same amount of 'stuff' in the UI, so whichever style you'd rather use should be fine.
Depending on how you want your UI to work you might consider link_to_remote (part of the prototype helper) - you can specify an action, params etc, and have it return some JS that gets run.
If you're using map.resources in your routes.rb you should be able to do something like this:
map.resources :things, :member => {:accept => :get, :reject => :get}
Then in your controller:
def accept
#thing = Thing.find(params[:id])
#thing.is_accepted = true
#thing.save
end
def reject
#thing = Thing.find(params[:id])
#thing.is_accepted = false
#thing.save
end
And finally in your view:
<%= link_to 'Accept', accept_thing_url(#thing) %>
<%= link_to 'Reject', reject_thing_url(#thing) %>
Or if you are using Ajax:
<%= link_to_remote 'Accept', :url => accept_thing_url(#thing) %>
<%= link_to_remote 'Reject', :url => reject_thing_url(#thing) %>