The following works great for carrying forward data from one page to another:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => session[:worequest_id] %>
How would I add a 2nd field? The following doesn't work:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => session[:worequest_id] = #worequest.id, [:client_id] = #worequest.client_id %>
Thanks!
UPDATED
This is the code I'm using in the new work order form. It picks up the worequest_id field from the session
<% if session[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => session[:worequest_id] %>
onclick doesn't really work this way – it's an html attribute used to store JavaScript code to be executed when the element is clicked. While you can use it to evaluate Ruby code in the context of a Ruby method call (in this case as part of the options hash given to link_to), it doesn't really make sense to do so.
In your first example, it doesn't actually do anything. If you check your rendered html on the page where that link appears, I expect it evaluates to something like New Work Order. You can, however, store data in session (which is persistent for as long as the user remains logged in), which is why you're seeing this data carrying forward from page to page.
If you're trying to fill in default values for the new workorder, you could pass them as params to the path method:
link_to 'New Work Order',
new_workorder_path('workorder[worequest_id]' => #worequest.id,
'workorder[client_id]' => #worequest.client_id),
:class => 'btn btn-primary'
In your workorders#new action, your model instantiation would need to include the params:
def new
#workorder = Workorder.new(params[:workorder])
end
However, this might not be the best way to proceed. If there will always be a client or worequest associated with a workorder, you might want to look into nested routes.
Related
i need to get a ajax tooltip on a dynamic link, so the logic seems to concatenate it. but, still not work, so, someone know a way to do this?
thank's
<%= link_to "Profile", edit_user_path(current_user), :class =>"ttooltip", :data => {:url => "/users/#{#current_user}/links"} %>
You're string interpolating the current user object, which will call .to_s on the user object, which probably isn't what you want.
If links is nested under each user, you typically follow the 'users/:id/links' so you need to interpolate the id instead of the user object like so:
<%= link_to "Profile", edit_user_path(current_user), :class =>"ttooltip", :data => {:url => "/users/#{current_user.id}/links"} %>
(Where current_user is a helper method that returns the current_user object.)
Here is the offending line:
<p><%= link_to 'Job Type template', :action => :jt_template, :style => "color:white", :class => "btn btn-primary" %></p>
And when we load the page and inspect the element:
Job Type template
Why are my class and style tags being interpreted as params for the action?
Other than this weirdness - it's working as expected.
Just FWIW - the jt_template action sends the CSV file as a download. I was linking directly to the file stored in my public dir, but I found that when you clicked the button in Safari it would just open the CSV in the browser, and I'm forcefully required to make the file download instead (even though a user could get from the opened page)
EDIT:
Ever do that thing where you post to SO, then think of some new wording you haven't Googled, then find your answer on the first result?
Add css class to rails link_to helper
Someone go ahead and post the answer "you should put your :action inside { } and it will work" and I'll accept that.
Do this:
<p><%= link_to 'Job Type template', {:action => :jt_template}, :style => "color:white", :class => "btn btn-primary" %></p>
:action and :controller must be placed between { } in order to avoid the incorrect interpolation of stuff that comes after it (I only tried with :class and :style, but I assume it would affect anything that follows)
Let's say I'm in a new action and want to pass some extra information to the create action (for instance, how many times a user has pressed a given button, :clicks).
How should I go about accomplishing the task?
Try:
<%= hidden_field_tag 'click_count', 0 %>
<%= submit_tag "Click me!", :type => 'button', :onclick => '$("#click_count").val(parseInt($("#click_count").val())+1)' %>
I have this form:
<%= form_tag posts_path, :method => :get, :class => "search_nav" do %>
<%= text_field_tag :search, params[:search], :class => "input-long search-query", :placeholder => "#{t('.search_nav')}" %>
<%= hidden_field_tag('ip', "#{request.ip}") %>
<%= hidden_field_tag('city', "#{request.location.city}") %>
<%= hidden_field_tag('country', "#{request.location.country}") %>
<%= content_tag(:div, "",:class => "icon-search") %>
<% end %>
I get a url something like:
http://localhost:3000/en/posts?utf8=%E2%9C%93&search=check+params&ip=127.0.0.1&city=&country=Reserved
My question is:
Can I hide or encrypt the url params ip, city and country?
I can not use POST because I have paginate results:
<a rel="2" href="/en/posts?city=&country=Reserved&ip=127.0.0.1&page=2&search=check+params&utf8=%E2%9C%93">2</a>
<a rel="3" href="/en/posts?city=&country=Reserved&ip=127.0.0.1&page=3&search=check+params&utf8=%E2%9C%93">3/a>
Encrypting URL parameters is pretty pointless. Why don't you want the user to see these values? Sure you COULD encrypt them before adding them to the form, but is that really necessary?
Furthermore, and perhaps more importantly, if these values are based on the request, then there is a good chance you don't need to submit them in the first place. #{request.xxx} is going to be the same on the result page as is on the form page. Is there any good reason to pass these along? By submitting these as GET parameters, you're actually sending redundant information to the server. Ruby/Rails is already going to calculate these values based off of the IP address automatically when the next page is loaded.
The problem here isn't with the form, but rather with the logic you've applied to designing it. I think you may have over-analysed your situation, and need to take a step back and re-think the problem.
Is there a way to control the *submit_tag* in the form to invoke different action to the default 'update' action?
I tried to use the submit_tag below, but it still redirect me to 'update' action in people controller.
<%= submit_tag "Save", :controller => "people", :action => "set_password", :method => "put" %>
The reason why I'm doing this is that,
I have two update forms for the Person class, one for updating the basic information, and one for updating the password. I would like to handle the form submit differently.
For 'updating password form', i have to something additional.
* validate the additional user input (current password)
* direct to 'update password' form if there is an error
Am I doing the wrong thing? Or I should distinguish the cases inside the 'update' method?
You have to tell the form where to go, not on the submit_tag:
<%= form_tag #object, url, :method => 'PUT' %>