Rails add dynamic attribute on data content helper link - ruby-on-rails

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.)

Related

Rails onclick => 2 items

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.

Search url with a nested hash of parameters

I used to have a link that lead to a search action and carried certain parameters. This was the link (for example):
<%= link_to "search", discover_search_url(:category_id => 3) %>
A friend of mine refactored the site and did a fantastic job of it, but now the category_id is inside a parent hash called pieces_search. So instead of using params[:category_id] now I use params[:pieces_search][:category_id].
My question is, how do I modify my link now?
<%= link_to "search", discover_search_url(:pieces_search => {:category_id => 3}) %>

Rails 3.2 button_to not passing GET variable

still learning rails, doing my first project.
i'm trying to pass an additional "category" variable to "new" method in my "pages" controller
def new
#page = Page.new
#cats = Cat.all
end
i'm doing it so the new page has already selected category from dropdown menu
it works when i use link_to
<%= link_to "Create new page", new_page_path(:cat => #cat.id) %>
but when i'm trying to use button_to
<%= button_to "Create new page", new_page_path(:cat => #cat.id), method: :get %>
the variable "cat" is not passed to "new" action view. it's not a big problem but it screws up my layout because i'm using button_to in all other places and i just hoped there is a better way to solve it that adding more css so it'll look the same
One possible way to solve this is to create a form with a hidden field
<%= form_tag new_page_path(), :method => :get do
hidden_field_tag "cat", #cat.id
button_to "Create new page"
end %>
Your version doesn't work because "button_to" method creates a form which passes params to the browser only from input fields.

Interpolated Ruby within a method call?

On my user model, I have a bunch of attributes like is_foos_admin and is_bars_admin that determine which kinds of records a user is allowed to edit.
I'd like to DRY out my edit links, which currently look like this:
<%= link_to 'Edit', edit_foo_path(foo), :class => 'edit' if current_user.is_foos_admin? %>
...
<%= link_to 'Edit', edit_bar_path(bar), :class => 'edit' if current_user.is_bars_admin? %>
I want to make a helper that lets me pass in a foo or bar and get back a link to edit it, like so:
<%= edit_link_for(foo) %>
The helper might look like this (which doesn't work):
def edit_link_for(thing)
if current_user.is_things_admin?
link_to 'Edit', edit_polymorphic_path(thing), :class => 'edit'
end
end
The model-agnostic edit_polymorphic_path method gets me halfway there, but it's the "is_things_admin" method that I don't know how to universalize. If I could use interpolated Ruby inside of a helper, I'd want to do something like
if current_user.is_#{thing.class.name.downcase.pluralize}_admin?
But of course that doesn't work. Any ideas?
Try using send:
if current_user.send("is_#{#model}_admin?")

Rails custom submit_tag

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

Resources