How to correct write this record? When I write the following code, which you can see in this post I have received following mistake: undefined method `stringify_keys' for "/plant/index":String
Html code
<li class="new_button">
<%= link_to "Plants", plant_path, :class=>"greens" do %>
<span></span>
<% end %>
</li>
I need (just with link_to)
<li class="new_button">Plants<span></span></li>
If you pass block into link_to, first argument is responsible for evaluating proper link href and second argument is assumed options. In your example, second argument is returned value of plant_path, which is String instance, but Rails want to evaluate it as Hash. What you should do is:
<%= link_to plant_path, class: 'greens' do %>
Plants<span></span>
<% end %>
Try this
<%= link_to plant_path, :class=>"greens" do %>
<span><%= "Plants" %></span>
<% end %>
Related
I have an index where users can apply many different kinds of filters through checkboxes.
Each filter has an "X" link_to button that dismisses the filter, which basically makes a request to the same index page permiting the current params with the exception of the dismissed filter's param key.
For example:
<div class="form-group <%= 'hidden' unless params.dig(:q, "subscription_payment_type_eq").present? %>" id="subscription_payment_type_eq" data-toggle-target="objective">
<%= f.select :subscription_payment_type_eq, payment_types_selector_for_filter, { include_blank: "Forma de pago..."}, class: "form-control gray my-2 " %>
<% if params.dig(:q, "subscription_payment_type_eq").present? %>
<%= link_to admin_users_path(params: params.permit(q: [:extra_params, :address_province_eq, :subscription_status_eq, :created_at_gteq, :created_at_lteq])) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
</div>
<div class="form-group <%= 'hidden' unless params.dig(:q, "address_province_eq").present? %>" id="address_province_eq" data-toggle-target="objective">
<%= f.select :address_province_eq, user_provinces_selector_for_filter, { include_blank: "Provincia..."}, class: "form-control gray my-2 " %>
<% if params.dig(:q, "address_province_eq").present? %>
<%= link_to admin_users_path(params: params.permit(q: [:extra_params, :subscription_payment_type_eq, :subscription_status_eq, :created_at_gteq, :created_at_lteq])) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
</div>
The approach works, but it's very cumbersome to manually add the excepted param key to each new filter that is added to the index page.
Is there a way to pass params excluding the param key that belongs to the filter being dismissed?
I've tried the following but no params are passed:
<% if params.dig(:q, "subscription_payment_type_eq").present? %>
<%= link_to admin_users_path(params: params[:q].except(:subscription_payment_type_eq).permit) do %>
<span class="dismiss-box glyphicon glyphicon-remove-circle">
<% end %>
<% end %>
Also tried this but got undefined method 'exclude' for #<ActionController::Parameters:0x00007f6331b00dd8>
<%= link_to admin_users_path(params: params.exclude(:subscription_payment_type_eq).permit) do %>
Permit expects a list of args and since you're not sending any no params will be passed to your controller. I'm actually not sure how your calling permit without args is even working. Anyway maybe you can try it like this instead:
<%= link_to admin_users_path(params: params[:q].except(:subscription_payment_type_eq.permit!)
In the end we decided to implement it this way:
<%= link_to admin_users_path(
params: { q: params.require(:q).permit(params[:q].keys - ["address_province_eq"]) }
) do %>
We basically substract the filter's key from the current params when passing it in the link_to
<%= link_to 'Received Messages<span class="badge badge info">
#receivedmessage.count</span>'.html_safe, '/messages/show' %>
below is the output i'm getting so far. I have to get the "message count number" inside the badge instead of "#receivedmessage.count"
You can use link_to ... do, which IMO is cleaner than newmediafreak's answer:
<%= link_to '/messages/show' do %>
Received Messages<span class="badge badge info"><%= #receivedmessage.count %></span>
<% end %>
When you want to interpolate your variable you should use quotes instead of single quotes. Also, you'll need to use #{} around your variable. An example:
name = 'John'
puts "Hello, #{name}!"
Notice how I've used quotes on the second line when using string interpolation and surrounded the variable name with #{}?
Now, let's apply it to your example:
<%= link_to "Received Messages <span class='badge badge info'>#{#receivedmessage.count}</span>".html_safe, '/messages/show' %>
Update
String interpolation and html_safe might not always be a good idea, but since your only interpolating a count it should be fine. A better option would be to use what Micha has suggested (using link_to ... do):
<%= link_to '/messages/show' do %>
Received Messages <span class="badge badge info"><%= #receivedmessage.count %></span>
<% end %>
I have a loop that creates a list of works from the modal work
//does work but want test to be <%= work. name %>
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to 'test', work %></li>
<% end %>
</ol>
//doesnt work but want it to
<ol class="meny-control mobile">
<% #works.each do |work| %>
<li class="" data-id="<%= work.id %>"><%= link_to '<%= work.name %>', work %></li>
<% end %>
</ol>
As you would guess the <%= work.name %> throws a syntax error. How do I correctly format the link_to to display each work.name as the 'path' && the anchor's inner html as work.name.
Being new to rails, I'm still really iffy on understanding documentation properly. Could you please reference from link_to() (if even there) where this format is explained so I use this for future referencing & understanding --also how to properly edit the stack question title for future similar question.
The error is because of the nesting of <% tags and I suppose you already are aware of that. To solve your problem use the following:
<%= link_to "#{work.name}", work %>
The #{} is used to interpolate variables, i.e. replacing variables with their values within the string literals as in link_to "#{work.name}" above where work.name will be replaced by the value work.name holds.
you don't need "#{}".
you can write this:<%= link_to work.name, work %>
<% #ticket.conversations.each do |c| %>
<section class="messages">
<%="<li> #{c.the_message} </li>" %>
</section>
<%end%>
I am trying to have rails write the HTML code for me so the output would look something like this:
<li>MESSAGE1</li>
<li>MESSAGE2</li>
<li>Next message here...</li>
I am going to style every nth element to have a different style to show what speaker it belongs to. But currently is just outputs straight text and escapes the HTML. How do I stop this escape?
To output you need to use <%= as follows within your <section> block:
<%= "<li> #{c.the_message} </li>".html_safe %>
But currently is just outputs straight text and escapes the HTML
You can use the html_safe method. Please refer to the "Extensions to String" topic in this document: http://guides.rubyonrails.org/active_support_core_extensions.html
Another option you can use is the raw helper(as pointed out by Stefan) which calls the html_safe for you. e.g.
<%= raw "<li> #{c.the_message} </li>" %>
You can also style your list items this way:
<li><%= c.the_message %></li>
Just based upon preference.
Try it this way:
<% #ticket.conversations.each do |c| %>
<section class="messages">
<li><%= c.the_message %></li>
</section>
<% end %>
Or if you don't want to repeat <section> every time:
<section class="messages">
<% #ticket.conversations.each do |c| %>
<li><%= c.the_message %></li>
<% end %>
</section>
How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?