Facing error while accessing submitted values please guide someone - ruby-on-rails

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

Related

AbstractController::ActionNotFound with form_tag in Rails

When I use form_tag as below then I get this error:
AbstractController::ActionNotFound at /quizzes/[whichever_quiz_id]
The action 'whichever_quiz_id' could not be found for QuizzesController
My code is as below:
erb file
<%= form_tag action: :add_questions do %>
<%= collection_select(:quiz, :id, Quiz.where.not(id: params[:id]), :name, :id, prompt: 'Select Quiz') %>
<%= text_field_tag(:input_quiz_questions, 'Question ids') %>
<%= submit_tag "Add" %>
<% end %>
So here I have a method in my QuizController called add_questions which takes a selected question from another Quiz and adds it to the present Quiz.
def add_questions
id = params[:id]
required_quiz_id = params[:quiz][:id].to_i #taken from collection_select
required_questions_ids = params[:input_quiz_questions].split().map { |s| s.to_i } #taken from text_field_tag
# remaining logic here
routes
resources :quizzes, as: 'tests' do
member do
get :add_questions
end
end
So why am I getting the above error here and how can I rectify it?
You should write after form_tag the url, not the name of the action in the option hash
<%= form_tag quizzes_add_questions_path, method: :get do %>

Params not being passed to new action

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

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

Redirect search form

I've followed this tuto to make a search form in my app.
It works well but it doesn't do exactly what I want
At the moment, I've to go to /search to see my result but I want that when a search is made, everywhere in my app, it's redirect on /search and the results are displayed.
My search controller :
def search
if params[:q].nil?
#idees = []
else
#idees = Idee.search params[:q]
end
end
My search form view
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
And my route :
get '/search', to: 'search#search'
It has to be a problem with my route but I don't know how to do this
Update your form as:
<%= form_for :search, url: search_path, method: :get do |f| %>
You may need to update you search action too to support nested params.

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();
});

Resources