Params not being passed to new action - ruby-on-rails

I have a form in the view:
<%= form_tag(new_admin_course_path(chichi: #provider.id), {method: :get}) do %>
<%= submit_tag I18n.t('views.courses.index.add_new') %>
<% end %>
#provider.id is equal to 1 (checked with Pry).
new_admin_course_path lands in courses_controller#new. From the action itself (with binding.pry help) I got the content of params and this is what I found:
pry(#<Admin::CoursesController>)> params
=> {"utf8"=>"✓", "commit"=>"Add course", "controller"=>"admin/courses", "action"=>"new"}
I'm not new in Rails, but I have no idea why this is happening and I'm not getting what I pass through the helper method.
Any idea why this is happening?

Since you're trying to pass your chichi parameter, you could use button_to, which basically creates an empty form (you can pass params through):
<%= button_to I18n.t('views.courses.index.add_new'), new_admin_course_path, method: :get, params: { chichi: #provider.id } %>

Use This one:
<%= form_tag(new_admin_course_path(chichi: #provider.id), method: :get) do %>
<%= submit_tag I18n.t('views.courses.index.add_new') %>
<% end %>

Related

Refinerycms: Can`t call controller from view pf different engine

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

how to link form_tag to button rails 4

I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});

How would I condense this rails form into one button?

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.

rails 4 update user profile by button

hy, i'm trying to update a single field in a user resouce. The field to update is 'locale', it is a string type.
i have triend with a form and it works:
<%= form_for current_user do |f| %>
<%= f.select :locale, [['En', 'en'], ['It', 'it']] %>
<%= f.submit %>
<% end %>
now i'm trying to update the same field using a link or a button:
<%= current_user.locale = 'fr' %>
<%= button_to "update" ,current_user,method: :patch %>
<%= button_to "update" ,user_path(current_user),method: :patch %>
but none of this work.
the problem is the request, infact the web server doesn't recive the current_user parameters:
{"_method"=>"patch",
"authenticity_token"=>"7X6QdD4DGxsXaETT86/8Ut4xyuOICaxirs4IjmZl7jY=",
"locale"=>"en",
"id"=>"1"}
There is no current_users parameters.
i have tried with the link_to but the problem is the same:
<%= link_to "update", current_user ,method: :patch %>
I have no idea. Can you help me?
The fact that you don't get the parameter sent back to server is the expected one since you don't use a form.
In order to pass the parameter back you have to:
use a form (as you did) or
pass the parameter in the url of the button: button_to 'update', user_path(param_name: 'param_value')
In the second case, you will have to search for the appropriate parameter in your action, ex params[:param_name]

Facing error while accessing submitted values please guide someone

Im new to rails, and I am not able to understand how I can get data from a submitted form.
This is my form registerduser.html.erb
<%= form_tag("/submitform", :method => "get") do %>
<%= label_tag(:q1, "id:") %>
<%= text_field_tag(:q1) %>
<%= submit_tag("Submit") %>
<% end %>
How do I get the submitted values in this customers controller action?
def submitform
#customers_values = params[:q1]
end
routes.rb
get "customers/submitform"
error:
No route matches [GET] "/submitform"
You set wrong form url. It should be:
<%= form_tag('/customers/submitform', method: :get) do %>
...
or:
<%= form_tag(controller: :customers, action: :submitform, method: :get) do %>
...

Resources