I have a two part question. I'm passing params to links in order to sort and filter in my Ruby on Rails app. Is this type of resulting url safe?
/realestates?column=salesprice&direction=desc
If not, what would be the best way to hide the column name?
The second part of my question is what's the best way to translate these params to different languages. I have i18n set up. This is an example of a link with parameters:
<%= link_to realestates_path(column: :salesprice, direction: 'desc') do %>
<i class="fa fa-caret-down"></i>Order by price
<% end %>
Thank you!
Related
I am not sure what the correct approach is for my situation:
I want to create a link_to pushing all checkboxes with value="1" into an array, or individually if array is not possible, but I am at a loss of how to express that?
<% #cards.each do |card| %>
<%= check_box("#{card.name}", card.id, {checked: true}) %><%= "#{card.name}" %>
<% end %>
(Rails 4.2)
After a long time of dead ends, trying to make it a 'clean' solution I ended up with this very dirty approach. But as they say, done is better than perfect:
Create a link_to that would include all the cards, but add one additional params: user_selected_cards = "".
Create a javascript that listens to for checking/unchecking of the checkboxes and reads the id associated with that specific checkbox. Then take that incoming info and add or remove it to the actual url that the link_to generates by finding the user_seletected_cards= portion in the url and add or remove the id depending on if isn't or already is added to the list after the equal sign.
Here (below) I'm able to set the params variable (i.e., params[:archive_id]), but I want to do the equivalent in a session variable and without relying on AJAX, which was offered as a solution to a previous question.
<%= link_to books_path(archive_id: archive.id) do %>
<li><%= "See books!" %></li>
<% end %>
Any ideas? Open to other approaches that achieve the desired result: clickable <li> to books_path that also sets the session[:archive_id]. Thanks for any help.
(Oh, and archive has_many books.)
Update: I realize writing a helper function is an option, but the trouble with that is doing so without over-writing the session[:archive_id] when iterating through #archives. Sorry, should've mentioned that earlier.
I have a model called Book and another one called Magazine.
They share the same index view, where pictures of the covers are shown.They are also displayed according to their appearance date, so instances of those two models are mixed in the view...
Each cover has a clickable tite, and leads the user to a description page for this particular book or magazine...
Now in my view,i want to be able to do something like :
<%= link_to document.title, "#{document.class.name.underscore}"_path(document) %>
So in the case of book, i want this line to be replaced by the path from book_path(document) when document is a book,and by the path generated by magazine_path(document) when the document is a magazine.
À la bash script syntax...
How would i realize this.
Thank you very much!
Try:
<%= link_to document.title, polymorphic_path(document) %>
Polymorphic path, when executed with a model, checks the class of passed model, brings it do underscored notation and executes model_name_path. Seems to be exactly what you need.
You can always do this with eval.
<%= link_to "Title", eval("#{document.class.name.underscore}_path(document)") %>
There is also send, which is cleaner, but also metaprogramming:
<%= link_to "Title", send("#{document.class.name.underscore}_path", document) %>
I am having a an issue.
<%= link_to "<button>Add</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
What if I want to add a ruby variable and some normal text the button? e.g. $25,- (where the $ and ,- are fixed and the 25 variable...I am quite new to this...sorry if this is too easy, but struggling. I tried a lot of options and googled for long time.
The correct helper you want for this is button_to.
To use a variable in the button text is defined as string iterpolation. In your example it could be something like:
<%= button_to "$#{cost}", new_admin_course_path %>
Check out the button_to api reference for more options.
you could do like
amount = 25 #amount is the ruby variable
<%= link_to "<button>Add $#{amount}</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
and I personally dont add <button></button> to the link and I rather use CSS to get the look and feel
Put your link inside a <button></button> tag. It'll be more readable.
And use variables interpolation in double-quoted strings to insert variable's value into string (the #{#variable_name} part)
<button>
<%= link_to "$#{variable_name}", new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
</button>
Using Rails 3.1.1
I am creating a travel guide that typically consist of "articles". In these articles I write about each place. Each article is about 500 words long and is saved as the attribute article.content in the database.
Now, I would prefer to be able to use Rails helper methods (i.e. from application_helper) and "link_to" within these articles. I can't use <%= %> simply because Rails will just interpret this as text in the article.
The main reason for me wanting to do so is to use smart internal linking (routes) and helper methods.
To clarify further:
Article is a model which has an content attribute.
a = Article.first
z = Article.last
a.content = "This is a long article where I want to place a smart link to z by using <%= link_to 'z article', article_path(z) %> and use my helper method largify so that I can <%= largify('this text') %> but I can't. What should I do?"
a.save
Is there a smart way of solving this?
<%= render inline: a.content, type: :erb %>
But beware of filling your database from untrusted sources -- someone can use it to place malicious code between <%= %>.