How to pass params parameter through link_to? - ruby-on-rails

In Controller
class FeedEntriesController < ApplicationController
def index
#search = FeedEntry.search(params[:is_star])
#feed_entries = #search.page(params[:page])
#app_keys = AppKey.all
end
end
In View - feed_entries/index.html.erb
<%= link_to "Stared", {:controller => "feed_entries", :action => "index", :is_star => false } %>
feed_entries table contain is_star:boolean attribute. So, I just want to pass the parameter is_star == true into the params[:is_star].
But the above code is not working. Please some one help me.
If, I click the Stared link. I am getting error like below in the browser:
undefined method `stringify_keys!' for "false":String

It looks like your FeedEntry#search method expects an argument that responds to #stringify_keys! methods, so I assume you should pass a Hash :
#search = FeedEntry.search(is_star: params[:is_star])
BTW link_to with 'controller' and 'action' options is old and verbose syntax, you should use a resourceful style:
link_to 'Stared', feed_entries_path
read more here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Related

Ruby on Rails link_to method to change db value

Hello I am trying to change a string value from "no" to "yes" in my Rails Postgresql db based on if a user clicks a yes or no button using the link_to method in my view. I have designated the default string value to "no" so I would like the value to change to yes if a user clicks the yes button . I would like assign the 'link_to method' to the yesmarried action in my controller.I =m not sure how to locate the the newcase in the yesmarried action and I keep getting the error Couldn't find Newcase without an ID
here is my controller
class NewcaseController < ApplicationController
def new
end
def create
#newcase = NewCase.new(newcase_params)
end
def yesmarried
#newcase=Newcase.find(params[:id])
self.married = "yes"
end
private
def newcase_params
params.require(:newcase).permit(:state,:first_name,:last_name, :dob, :email, :telephone_number, :addr,:respondent_fname, :respondent_lname,:respondent_addr, :marriage_date, :state_of_marriage, :date_of_seperation, :number_of_children, :children_addr, :occupation , :work_addr, :net_monthly, :married)
end
end
here is my view:
<%=link_to 'yes',new_path(#newcase), class:"btn btn-lg rounded btn-warning", action: "yesmarried",method: "put", "data-toggle" => "modal", 'data-target' => '#married-question', "data-bs-dismiss"=>"modal" %>
routes file:
get '/new', to:'newcase#new'
post '/new', to:'newcase#create'
patch '/new', to:'newcase#yesmarried'
put '/new', to:'newcase#yesmarried'
Your code is a little unrestful and non-idiomatic but your yesmarried method should read:
def yes_married
#newcase = Newcase.find(params[:id])
#newcase.update_attribute(:married, 'yes')
end
Add the route:
get '/newcase_yes_married`, to: 'newcase#yes_married'
You'd need to change new_path to newcase_yes_married_path (or whatever path rails routes returns in the terminal).
However a better solution would be to piggyback an update method to keep it RESTful.
def update
#newcase = Newcase.find(params[:id])
#newcase.update_attributes(newcase_params)
end
<%= link_to "Yes", [#newcase, { newcase: { married: 'yes' }}], method: 'patch' %>
Note: code not tested.

Rails passing a list of objects to another controller with link_to_remote

Is it possible to pass a list of objects to another controller in Ruby on Rails?
I have experimented with the following example:
class Student
...
end
student_list = [std1, std2, ... stdn]
When I use
<%= link_to_remote("example", :url => {:controller => 'class/assignment',
:action => 'homework',
:student => student_list})%>
It did not work the way I expected. params[:student] is equal to "student" (string literal).
Is there anything I did wrong, or an alternate way of doing it?

Passing local variable to a partial in rails 2.x

I have the following method in my controller:
def update_fees_collection_dates_voucher
#batch = Batch.find(params[:batch_id])
#dates = #batch.fee_collection_dates
render :update do |page|
page.replace_html "fees_collection_dates", :partial =>"fees_collection_dates_voucher"
end
end
the method calls the following partial in _fees_collection_dates_voucher.html.erb file:
<%= select :fees_submission, :dates_id, #dates.map { |e| [e.full_name, e.id]},{:prompt => "#{t('select_fee_collection_date')}"},{:onChange => "#{remote_function( :url => {:action => "load_fees_voucher"},:with => "'date='+value+'&batch_id='+#batch.id") }"} %>
The partial makes a remote call to the load_fees_voucher method when a selection list value is selected or changed. However, I'm unable to pass the information in the #batch instance variable (from my original method) to the remote method via the partial. If I change the #batch.id to a hard-coded value in the last line (under :with) the code runs fine but not in the current format. Is there a different procedure to access instance variables in partials ? Thanks!
You can use the same instance variable in the partials.
Try this,
<%= select :fees_submission, :dates_id, #dates.map { |e| [e.full_name, e.id]},{:prompt => "#{t('select_fee_collection_date')}"},{:onChange => "#{remote_function( :url => {:action => "load_fees_voucher"},:with => "'date='+value+'&batch_id='+#{#batch.id}") }"} %>

How to “dynamically add options” to 'form_for'?

I am using Ruby on Rails 3.2.2. In order to implement a "dynamic generated" AJAX style file upload form I would like to "dynamically add options" to the FormHelper#form_for statement if some conditions are meet. That is, at this time I am using code as-like the following (note that I am using the merge method in order to add options to the form_for method):
<%
if #article.is_true? && (#article.is_black? || && #article.is_new?)
form_options = {:multipart => true, :target => "from_target_name"}
else
form_options = {}
end
%>
<%= form_for(#article, :remote => true, :html => {:id => "form_css_id"}.merge(form_options)) do |form| %>
...
<% end %>
However, I think that the above code is too much hijacked.
Is there a better way to accomplish what I am making? For example, can I access from view templates some (unknown to me) instance variable named as-like #form and "work" on that so to change related options as well as I would like? Or, should I state a helper method somewhere? How do you advice to proceed?
BTW: Since the upload process is handled by using a HTML iframe, I am using the remotipart gem in order to implement the AJAX style file upload form - I don't know if this information could help someone...
This looks like a good candidate for a helper method. In your view:
<%= form_for(#article, :remote => true, :html => article_form_options(#article, :id => "form_css_id")) do |form| %>
...
<% end %>
In app/helpers/articles_helper.rb
module ArticlesHelper
def article_form_options(article, defaults = {})
extras = if article.is_true? && (article.is_black? || article.is_new?)
{ :multipart => true, :target => 'form_target_name' }
else
{}
end
defaults.merge(extras)
end
end
Helpers are a good place to keep logic that's too complex for a view but still related to the view.

No route matches show action error thrown after submitting form with select_tag

I have a select_tag in a form within my Rails 3 app. When I select a vacation, and the form is submitted, I'd like to be routed to the show action on my vacations_controller. Here is the code for my form:
<%= form_tag url_for(:controller => "vacations", :action => "show"), :method => 'get', :id => "song_selector" do %>
<%= select_tag "vacation_id", options_for_select([["Choose your vacation", ""]]+ Vacation.active.collect {|vacation| [ vacation.title, vacation.id ] } ) %>
<% end %>
However, when I try that, I get an error:
No route matches {:controller=>"vacations", :action=>"show"}
I definitely have a route for this:
resources :vacations, :only => [:index, :show]
And the output of rake routes:
vacation GET /vacations/:id(.:format) vacations#show
I know from previous answers that I'm just not passing the ID in the URL as expected. If I raise params it looks like my ID is being passed as a string like so: `"vacations" => "2".
So I'm wondering: How I can construct my select_tag so this is fixed?
You're missing the id in that action.
Try:
<%= select_tag "id", options_for_select([["Choose your vacation", ""]]+ Vacation.active.collect {|vacation| [ vacation.title, vacation.id ] } ) %>
But this will not be ideal either, as the url will likely be something like "/vacations/?id=X".
An alternative is to use javascript and build the url based on the select option, that way you can construct the url the way you like it.

Resources