rails search nested resource - ruby-on-rails

i try to search something in my objet calendar but all the time when i click on submit i get this error.
And i dont really understand why ?
The request method POST is inappropriate for the URL /.
_search.html.erb
<%= form_for user_calendars_path([#user, #calendar]), :method => 'get' do %>
<%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
<%= submit_tag "Search" %>
<p>hello </p>
<% end %>
calendar_controller
def index
#search = #user.calendar.search(params[:search_condition])
#content_calendars = #user.calendar.all
#content_calendars_by_dates = #content_calendars.group_by(&:published_on)
#date = params[:date] ? Date.parse(params[:date]) : Date.today
end
route.rb
resources :users do
resources :calendars
end

Try this:
<%= form_tag user_calendars_path(#user, #calendar), :method => 'get' do %>
<%= text_field_tag :search, params[:search_condition], :id => 'search_field' %>
<%= submit_tag "Search" %>
<p>hello </p>
<% end %>

Related

Passing parameter to get member through simple_form

I'm trying to implement a search function but can't figure out what simple_form_for needs to match up with the routes:
resources :shows do
member do
get :search
end
resources :episodes
end
I've tried a few different formats:
# views/shows/_search.html.erb
<%= simple_form_for :search, url: search_show_path(show), :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit, "Search", class: "btn btn-default" %>
<% end %>
<%= simple_form_for search_show_path(show), :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit, "Search", class: "btn btn-default" %>
<% end %>
<%= simple_form_for search_show_url(show), :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit, "Search", class: "btn btn-default" %>
<% end %>
When I place a string in the controller in place of params[:search] the query works, so I'll leave that code out. I'm calling the form with <%= render 'search', show: #show %> in views/shows/show.html.erb.
Since you are expecting an object in params[:id], this means its a member route and not a collection one.
Change your routes file to:
resources :shows do
member do
get :search
end
resources :episodes
end
Debug Hint: Always see whats the URL that is getting built (e.g. http://localhost:3000/shows/search.6?utf8=%E2%9C%93&search%5Bsearch%5D=test&commit=Search) in this case.. 6 was not getting passed in the URL correctly.. thus an issue with path helpers.

RoR: form_tag values to controller

I am again stuck on my road to learn RoR, now with form_tag. I followed the rails guide but not able to pass the parameters from form_tag to controller. My index.html.erb is:
<h1>Welcome to mySite.com</h1>
<p></p>
<p></p>
<%= form_tag(controller: "logins", method: "post") do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%= submit_tag "create" %>
<%= submit_tag "clicked" %>
</p>
<% end %>
Controller looks like:
class LoginsController < ApplicationController
def index
end
def create
if params[:commit] == 'clicked'
render action: "clicked"
else
render text params.inspect
end
end
def clicked
render text: params.inspect
end
end
routes.rb
match "logins/index" => "logins#index", :as => :index , :via => [:get, :post]
#get "logins/create"
match "logins/create" => "logins#create", :as => :create ,:via => :get
#match "logins/clicked" => "logins#clicked", :as => :clicked, :via => [:get, :post]
get 'logins/clicked', to: 'logins#clicked'
#resources :logins
Button click on index.html.erb refreshes the page like brings me back to index page.
Update: Updated with the suggestions in the comment.
Thanks
Abhi
<h1>Welcome to mySite.com</h1>
<p></p>
<%= form_tag(controller: "logins", action: "create") do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%=submit_tag "Create"%>
<%= submit_tag "Clicked" %>
</p>
<% end %>
Controller looks like:
class LoginsController < ApplicationController
def index
end
def create
if params[:commit] == "Clicked"
p "hi"
redirect_to clicked_logins_path(request.parameters)
else
render text: params.inspect + "hi"
end
end
def clicked
render text: params.inspect
end
end
in routes.rb
resources :logins do
collection do
get 'clicked'
end
end
i hope it helps.
you need to give submit path in your form_tag
Welcome to mySite.com
<%= form_tag(clicked_login_path, method: "post" ) do %>
<p>
<%= label_tag(:username, "Username") %><br>
<%= text_field_tag(:username) %>
</p>
<p>
<%= label_tag(:password, "Password") %><br>
<%= password_field_tag(:password) %>
</p>
<p>
<%= submit_tag 'submit'%>
</p>

Ruby on rails: Multiple submit_tag on the same form

How can I have multiple submit_tag buttons on the same form?
For now I got it working only for one button, but I'm not sure how to get form_tag to handle multiple paths.
Routes.rb
resources :actions do
end
root 'home#start'
match '/home/add', to: 'home#add', via: 'get'
match '/home/subtract', to: 'home#subtract', via: 'get'
match '/home/multiply', to: 'home#multiply', via: 'get'
Start.html.erb
<%= form_tag "/home/add",:method => "get" do %>
<p></p>
<p>
<%= label_tag :entered, "Please enter value:" %> </br>
<%= text_field_tag :entered %>
</p>
<p></p>
<p>
<%= label_tag :entered2, "Please enter value:" %> </br>
<%= text_field_tag :entered2 %>
</p>
<%= submit_tag "add", :controller => "home", :action => "add" %>
<%= submit_tag "subtract", :controller => "home", :action => "subtract" %>
<%= submit_tag "multiply", :controller => "home", :action => "multiply"%>
<% end %>
Please advise.
Thank you in advance.
I don't know if you can make it go to different path. But would something like this help?
Just have one action and do stuff in your controller based on the submit button that was clicked. You routes will look like
*Routes.rb*
resources :actions do
end
root 'home#start'
match '/home/operation', to: 'home#add', via: 'get'
You view will be
<%= form_tag "/home/operation",:method => "get" do %>
<p></p>
<p>
<%= label_tag :entered, "Please enter value:" %> </br>
<%= text_field_tag :entered %>
</p>
<p></p>
<p>
<%= label_tag :entered2, "Please enter value:" %> </br>
<%= text_field_tag :entered2 %>
</p>
<%= submit_tag "Add"%>
<%= submit_tag "Subtract"%>
<%= submit_tag "Multiply"%>
<% end %>
In your controller
class HomeController < ApplicationController
def operation
send(params[:commit].downcase) #params[:commit] will have one of the values "Add", "Subtract", "Multiply"
end
private
def add
#do something
end
def subtract
#do something
end
def multiple
#do something
end
end

link submit to another page

I have this form:
in header of my website:
<% form_tag request.path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
controller:
def index
#title = "All users"
#users = User.paginate(:page => params[:page])
#users1 = User.simple_search(params[:query]).all
end
model:
acts_as_simply_searchable :columns => [:name, :email]
view
<%= will_paginate %>
<ul class="users">
<%= render #users1 %>
</ul>
<%= will_paginate %>
displays a user
I want to link the submit button to index.html.erb (i have assigned a path in routes.rb). So that the user can look at the search results
You don't do this at the submit button, but at the form_tag URL (the first parameter). It would be something like this:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search User", :name => nil %>
</p>
<% end %>
Simply change request.path to the desired path.
I think you are missing the '=' sign and string interpolating the 'request.path'
This Worked for me:
<%= form_tag "#{request.path}", method: "get" %>
...the rest of your code ...
<% end %>

remember form tag input values

I'm trying to create a filter form that user selects dates and sources. The problem is after clicking submit button, in the new page i see that input values are empty. Is there a way to make form remember its values ? Thanks.
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from %>
<%= text_field_tag :to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>
controller
def index
#from = params[:from] ? params[:from].to_datetime : (Time.now-3.day)
#to = params[:to] ? params[:to].to_datetime : (Time.now)
#sources = params[:sources] ? params[:sources] : 1..6
#products = Product.where(:source_id => #sources, :created_at => #from.beginning_of_day..#to.end_of_day)
end
Can't you use the value and checked options from these tags? Here is an example :
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from, #from %>
<%= text_field_tag :to, #to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id, #sources.include?( source.id ) %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>

Resources