Ruby on Rails: How do I pass variables with observer_form? - ruby-on-rails

I have
<%= observe_field :tb_search,
:frequency=>0.5, :update=>'reports',
:url=>{
:action=>'filter_reports',
:only_path=>false,
:user=>#user.id},
:with=>"'search='+encodeURIComponent(value)" %>
but on the controller side, params[:user] is nil.
I did a
puts "NTHDEONUTHDOEDEUU#{params[:user].nil?}||"
in the responding method in controller to prove to myself that it was nil.
any ideas?

With this helper I believe you need to pass all params through the :with option. Something like:
<%= observe_field :tb_search,
:frequency=>0.5, :update=>'reports',
:url=>{
:action=>'filter_reports',
:only_path=>false},
:with=>"'search='+encodeURIComponent(value)+'&user='#{#user.id}"

Related

How do I access filterrific scope variables directly in Ruby on Rails?

I'm using Ruby on Rails and I'm trying to access a filterrific scope variable from a 'select' drop down in the view for use in the controller.
Basically, my code looks like this.
<%= form_for_filterrific #filter do |c| %>
<%= c.select(:departments, #filter.select_options[:docs], {}, {style: "width: 140px;"} ) %>
<% end %>
Is there a way for me to get the currently selected doc option from the drop down and send it to a controller?
After form was submitted you can get selected option in controller from params, something like:
params[:filterrific][:departments]
In any case try inspect params in controller to investigate:
puts params.inspect

Issue with passing params in link_to rails

I'm having trouble passing a param through a link_to in Rails using the below code:
<%= link_to new_registration_path, {:workshop => #workshop.id } do %>
When I pry into the controller, the :workshop is not being included in the params (only controller and action).
Is this a strong params issue?
The workshop param has to be passed to the new_registration_path helper instead of passing it to link_to, like this:
<%= link_to new_registration_path(workshop: #workshop.id) do %>
If you want the URL to be like /something/123 instead of /something?workshop=123, you can change how your route is defined on routes.rb:
get something/:workshop
and then you can pass workshop: 123 to the URL helper.

getting an undefined method error from "_user.html.erb" in Rails 3

If I am getting this error
undefined method impressionist_count' for nil:NilClass after trying to use this line inside _user.html.erb
<%= #user.impressionist_count+#user.microposts.sum(&:impressionist_count) %>
where does it need to be defined? I already have this inside user.rb
def impressionist_count
impressions.size
end
I tried it in various helpers but to no avail
When you "render #users" each user is accessible to the partial as the local variable "user". Your partial should look like:
<%= user.impressionist_count+user.microposts.sum(&:impressionist_count) %>
The method is defined, but the #user object is not. This should've been assigned in the controller in order for it to work inside your view, or your partial should've been provided an object when you're rendering it.
Maybe you have something like:
<%= render(:partial => 'user') %>
What you might need is this if you have a user variable:
<%= render(:partial => 'user', :object => user) %>
Errors including the phrasing "for nil" are an indication of an undefined value being used incorrectly.

Rails - escaping backslashes in params causing havoc?

I have a form that passes the same parameters as the form before it:
<%= form_tag({:controller => "workouts", :action => "random"}) do %>
<%= hidden_field_tag :workout, params[:workout] %>
<%= hidden_field_tag :time, params[:time] %>
<%= submit_tag "Get Another", :class => 'btn' %>
The first form works fine, the second form to "get another" gives me the error can't convert Symbol into Integer for this line:
#equipment_ids = params[:workout][:equipment_ids].collect{|s| s.to_i}
The params of the first and second form being passed are:
{"utf8"=>"✓",
"authenticity_token"=>"qj/Q/YWvLKK3A3paAnEom4oTFtq44daX6dvEb8qmgtE=",
"workout"=>{"equipment_ids"=>["",
"508",
"518"]},
"time"=>"25",
"commit"=>"Get Workout"}
{"utf8"=>"✓",
"authenticity_token"=>"qj/Q/YWvLKK3A3paAnEom4oTFtq44daX6dvEb8qmgtE=",
"workout"=>"{\"equipment_ids\"=>[\"\",
\"508\",
\"518\"]}",
"time"=>"25",
"commit"=>"Get Another"}
The only difference is the escaping backslashes. I'm not sure why these would cause a problem?
Changed the hidden field tag to:
<%= hidden_field_tag "workout[equipment_ids][]", params[:workout][:equipment_ids] %>
I just went into the same problem when trying to manually submit a form with a custom POST request. The problem seems to be that net/http post_form method can only handle a single hash where all the values are Strings. If you have hash inside hash (like in the form that scaffold generates), it treats the inner hash as a String, and adds the nasty backslashes that, as you just saw cause havoc :)
The solution for me was to use the lower level "post" method, and to manually encode the hash. Define this module:
module HashToHttpParams
def to_http_params
map do |k, v|
if v.is_a?(Hash)
v.map do |kk, vv|
"#{k}[#{kk}]=#{vv}"
end.join('&')
else
"#{k}=#{v}"
end
end.join('&')
end
end
And then add it to the Hash class in your code:
Hash.send(:include, HashToHttpParams)
Finally encode your params hash before using it. In my code this looks like:
Net::HTTP.start("localhost",3000) do |http|
http.post("/tests", params.to_http_params)
end
Don't know if there's a better solution, but this worked for me.
Source: http://porras.lacoctelera.net/post/2007/10/08/enviando-formularios-con-parametros-compuestos-con-ruby-y-net#c4300080
As Hallucynogenyc pointed out, this is caused by the .post_form (docs) method only wanting a non-nested hash that is strings. I had this same problem, and solved it by switching to use the .post method.
require "net/http"
uri = URI('http://www.yoururl.com')
http = Net::HTTP.new(uri.host)
response = http.post(uri.path, params.to_query)
The .to_query method is also useful for converting hashes.
Another way to solve it is to not use the rails form method to create your params. If you just use straight html, for some reason the .post_form method likes it better.
Email <input name="student_email" type="email" autofocus="true">

link_to problem

I want to display product count in a link_to, the link_to is a part of partial displayed in application.erb.html, the problem is, I have a method in my application controller named products_on_cart which return products count, when I try this code:
<%= link_to "<%= products_on_cart%>", :controller=>"carts", :action=>"index"%>
rails give me an error:
"syntax error, unexpected '>'
...er=>"carts", :action=>"index"%>"
I don't really understand why, can somebody help me?
You can't use <%= .. %> inside of <%= .. %>.
<%= link_to products_on_cart, [:carts] %>
You're nesting ERb tags. Make sure products_on_cart() is available as a helper method, then rewrite your link_to code without nested ERb tags as follows:
<%= link_to products_on_cart(), :controller => "carts", :action => "index" %>
To make products_on_cart() a helper method, either move it to app/helpers/application.rb, or declare it as a helper in your controller:
def products_on_cart()
# method definition goes here
end
helper_method :products_on_cart
If you only need to access products_on_cart from your views and not from your controllers, putting it in app/helpers/application.rb is the preferred way to go. If you need to use it in both controllers and views, use the helper_method approach above instead.

Resources