Link to helper for a dynamic url path in rails - ruby-on-rails

I have the following code in my rails application,
<% = link_to variant.name, add_coaching_product_path(:variant => variant) %>
I want the above path add_coaching_product_path to send in a variable, and use in rails view.
I tried the following way, but it didn't work.
<% = link_to variant.name, "#{add_coaching_product_path}(:variant => #{variant})" %>

Note: add_coaching_product_path is kind of method created by Rails routes.
You can do it as below,
<% = link_to variant.name, send("add_coaching_product_path", { variant: variant }) %>
Instead of "add_coaching_product_path", you can pass dynamic path in form of string.

Related

Can I nest a ruby embed symbol <%= %> inside another <%= %>

I want to use a helper Method inside link_to tag as follows
<%= link_to "Home", root_path, class:"nav-link <%= activeLink('home') %>" %>
This is my helper method
def activeLink(action_name)
if controller.action_name == action_name
"active"
end
end
I am getting error saying;
ActionView::SyntaxErrorInTemplate in PagesController#about
I want that helper method to check the current action name and
set the 'active' CSS class if current action matches the input action name
Can you use nested <%= ... %> notation? No.
However, what you're looking for is a form of string interpolation. As mentioned in the comments Ruby variables can be converted to strings in a couple ways (full details outlined in the linked guide).
The primary method you'll see is by using #{} within a string ie "This is my string #{ruby_variable}".
Which means you could use the following:
my_string = "World!"
hello_world_string = "Hello #{my_string}"
hello_world_string
=> "Hello World!"

How to manually set a controller variable from a Rails view

I'm trying to enter a URL from the view and pass it into my controller and set it equal to a variable. I'm trying it like this currently but it just keeps returning nil.
This is Controller/posts:
def run
url = params[:url_toget]
puts url
doc = Nokogiri::HTML(open(url))
post = Post.create do |post|
post.body = doc.at_css("body")
post.url_toget = url
end
end
This is Views posts:
<div class="form-group">
<%=text_field :url_toget, class:"form-control", placeholder:"Recipe URL" %>
</div>
<%= link_to "Rake", posts_run_path %>
How can I input a URL and have it pass to equal that variable?
You can't directly set variables from the view to the controller but you can post the request from the view.
Like:
<%= form_tag( '/run' ) do %>
<%= text_field_tag 'my_url' %>
<%= submit_tag %>
<% end %>
Then in your controller run action access the parameter: p params[:my_url]
Remember to enable the POST method for the run action in routes: post 'run', to: 'your_controller#run'
In my case I needed to name a form based on the Controller Action.
The way I did it was to do the following:
This is in HAML
%h2 #{params[:action].capitalize} Branch
If you wanted to do it in HTML you would do the following:
<h2>#{params[:action].capitalize} Branch</h2>
This resulted in the form being named Edit Branch when I was editing and New Branch when I was using the NEW action etc.
I hope this helps someone else.
Scott

Ruby on Rails: Store array value in variable and use as string

I'm in the process of refactoring some code. I'm trying to use arrays in my view as part of a for loop that makes columns in a table.
I have defined the arrays in my controller:
subjects_controller.rb
def index
...
#CRFS_TO_VIEW = [Baseline, TreatmentCompletion]
#CRF_PATH = {Baseline => 'baseline_path', TreatmentCompletion => tc_path}
end
So my goal; as the function iterates over #CRFS_TO_VIEW, the correct path is selected from #CRF_PATH and appended to the link_to function.
indext.html.erb
<% #CRFS_TO_VIEW.each do |crf| %>
<% path = #CRF_PATH[crf] %>
<%= link_to "edit", path(crf.where(subject_id: sub.subject_id).first %>
<% end %>
I also tried :
<%= link_to "edit", #CRF_PATH[crf](crf.where(subject_id: sub.subject_id).first %>
Which didn't work. I feel I must be getting close, any help or insight would be greatly appreciated.
Thanks.
A few things:
a. You should save yourself some time and loop through the dictionary instead of the array:
<% #CRF_PATH.each do |crf, path| %>
...
<% end %>
b. You are getting a string from the loop - you can invoke the equivalent method with send:
<%= send(path, ...) %>
c. You can simplify your retrieval of the objects using:
crf.find_by(subject_id: sub.subject_id)
That said - this seems like a pretty bad way of doing things. I'd recommend instead adding a view helper:
def crf_path(crf)
case crf
when Baseline then baseline_path(crf)
...
end
With something like this you could use (notice changed the find_by to find_by! for safety as well):
<% #CRFS_TO_VIEW.each do |crf| %>
<%= link_to "edit", crf_path(crf.find_by!(subject_id: sub.subject_id) %>
<% end %>
Finally instance variables should NOT be named upper case. If you want to use a constant define it as a constant (otherwise use lower case names).

Using a custom tag helper for ruby on rails with a bootstrap navbar

My problem is that I can't get this helper tag to display at all.
So in application_helper.rb I want to have a <% nav_link(name, path) %> tag helper to append bootstrap's .active class dynamically.
My code is the following:
def nav_link(name, path)
content_tag(:li, :class => active_class(path)) do
link_to name, path
end
end
def active_class(path)
(current_page?(path) ? "active" : "").html_safe
end
and I would like to use it like so
<% nav_link("Users", users_path) %>
My hunch is that there's a variable somewhere that's not properly sanitized. How do I fix this? Is the html_safe call necessary?
Unless it's a typo, you should use <%= nav_link("Users", users_path) %>.
Without =, nothing will be displayed
I think that your problem is that you have written <% nav_link(name, path) %> this executes the code but doesn't print anything.
It should be <%= nav_link(name, path) %>
I highly recommend this Gem, it will do exactly what you want.
https://github.com/vigetlabs/nav_lynx
And here is a method it provides:
<%= nav_link_to 'Page', my_path, {}, { :wrapper => 'li' } %>

Link_to doesn't work when using acts-as-taggable-on with custom method

Or rather I don't know how to specify the route for it.
I have my controller setup us:
def tags
#clients = current_user.clients.find_tagged_with(params[:tag])
end
and my views
Tags:
<% for tag in #client.tags %>
<%= link_to tag.name, clients_path(:view =>'tag', :tag => tag.name) %>
<% end %>
Only problem is that the link (clients_path) goes back to index and not 'all.' I know it has to do with changing the clients_path to somehow tell it to use 'all'. But I don't know how.
Any help?
Thanks
You can check your routes using rake routes.
I'm not sure what you mean by 'all' but if this is a custom method added to routes, then you should be able to use all_clients_path instead of clients_path.

Resources