Passing parameter to get member through simple_form - ruby-on-rails

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.

Related

Rails Nested routes undefined method

I keep getting an
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
when navigating to my new order path url /users/1/orders/new
Been stuck on this, not sure what the deal is.
Routes:
devise_for :users, :path => 'accounts'
resources :users, only: [:index, :show] do
resources :orders
end
root index:
<%= link_to "Create an Order", new_user_order_path(current_user.id) %>
form:
<%= form_for([#user, #order]) do |f| %>
<%= f.hidden_field :user_id %>
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<% end %>
new:
<h1>New Order</h1>
<%= render 'form' %>
<%= link_to 'Back', user_orders_path(#user) %>
When you write this:
<%= form_for(#order) do |f| %>
Rails is looking for orders_path, but you don't really have such a route as you have defined your orders resource nested under the users resource. So, you have to pass both: #user and #order to the form like this:
<%= form_for([#user, #order]) do |f| %>
# your code goes here
<% end %>
If you write the form this way, then Rails will look for this path: user_orders_path which will match your defined route and will work and you won't get this error anymore:
undefined method 'orders_path' for #<#<Class:0x007faefee50a88>:0x007faefee414e8>
So, this will fix your current issue. But, there is another issue in your new.html.erb file where you currently have this:
<%= link_to 'Back', user_orders_path %>
You have to pass the #user as an argument to this method:
<%= link_to 'Back', user_orders_path(#user) %>
otherwise, it will fail again.
Hope this helps to solve your problems.
Since orders is nested on top of users, the form_for declaration will need to include both the user, and the order objects.
<%= form_for([#user, #order]) do |f| %>
Your new template will also need to have a reference to the given user.
<%= link_to 'Back', user_orders_path(#user) %>

form_tag with same params as current rails

How do I create a form_tag with the same current params but an additional param?
<%= form_tag(params.merge( :controller => "integrations", :action => "show" ), method: :get) do %>
<%= text_field_tag :params, params[:q] %>
<%= submit_tag "Search", class: "btn btn-default"%>
<% end %>
where my url looks like:
http://localhost:3000/integrations/64?utf8=%E2%9C%93&q%5Bbio_or_topics_or_title_or_company_cont_any%5D=mcmaster&q%5Btitle_cont%5D=&q%5Bcompany_cont%5D=&q%5Blocation_cont_any%5D=&sort_by=twitter_followers&q%5Bfacebook_username_not_eq%5D=&q%5Btwitter_username_not_eq%5D=&q%5Blinkedin_username_not_eq%5D=&q%5Blocation_not_eq%5D=&commit=Search
Thanks!

How to implement a destroy confirmation form with simple_form?

I'm using Ruby on Rails 4 with simple_form. On my delete page, I would like to display a form confirming the user's decision to destroy the resource in question. Here is the code I am using:
<h2>Delete Page</h2>
<p>Are you sure you want to delete <%= #journalEntry.title %></p>
<%= simple_form_for(#journalEntry, :action => 'destroy') do |f| %>
<%= f.button :submit %>
<% end %>
However, this is getting processed by the update action instead (my server console shows that it is being sent as a PATCH request).
I also tried amending that code to the following, but with the same result:
<%= simple_form_for(#journalEntry, :url => {:action => 'destroy', :id => #journalEntry.id}) do |f| %>
<%= f.button :submit %>
<% end %>
Add :method => :delete option with simple_form_for :
<%= simple_form_for(#journalEntry, :method => :delete) %>
<%= f.button :submit %>
<% end %>

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

rails search nested resource

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

Resources