Adding a Class to a link_to Helper With Only 1 Parameter - ruby-on-rails-4

I have the following:
<% #users.each do |user| %>
<% if user.profile %>
<%= link_to user do %>
<h2><%= user.profile.first_name %> <%= user.profile.last_name %></h2>
<% end %>
<% end %>
<% end %>
The above code works fine. What this code does is that it will output the first and last names of every user. These names are clickable and will take me to that user's page. My main issue is with the 3rd line. The issue I am having is that I am trying to get rid of the link underline, but I am unsure as to how to pass a class into it. Below is my attempt. My class "no-text-dec" is just one line of "text-decoration: none;"
<%= link_to (user, class: "no-text-dec") do %>
I'm new to Rails, but I understand that link_to has a body, url options, and then html options in that specific order, but how can I make it work in this case? The above line makes my application is crash, but it's the only thing I can think of that makes sense. I'm assuming it's because I am not giving it its body argument, but I'm not sure what that would be.

This should work fine if user contains url/path correct
<%= link_to(user, class: 'some_class') %> do
<span>Delete</span>
<% end %>
The space after method in sending argument in helper method link_to is crashing your application
you can give a try at irb
def test(a,b)
puts a; puts b;
end
test ("Ad","Cd")
It should throw an error

Related

Globalize accessors on subset of available locales

By design, some classes will deal with only a subset of available languages.
the globalize-accessors gem is quite useful, however, the rendering requires that the following be defined
Class.globalize_attribute_names
so while available_locales = [:en, :ru, :fr, :de], the goal is to work with a smaller array [:en, :ru]
The documentation states Calling globalize_accessors with no options will therefore generate accessor methods for all translated fields and available languages. But the purported way to invoke is in the model
globalize_accessors :locales => [:en, :fr], :attributes => [:title]
How can the globalize_accessorsmethod refer to an array, something generated by the likes of
#post.owner.ownerlocales.pluck('locale')
(although the array values are quoted...)
A working solution found but that does not address the above question, is based on the fact that globalize-accessors
gives you access to methods: title_pl, title_en, title_pl=, title_en=
Thus, a controller method that generates a whitelist
#locales = []
#post.owner.ownerlocales.each do |ol|
locale = ol.locale
#locales.push(locale)
end
... then process in the view filtering out the globalize_processors from whitelist
<% Post.globalize_attribute_names.each do |lang| %>
<% my_string = lang.to_s %>
<% #locales.each do |locale| %>
<% checkstring = "_" + locale %>
<% if my_string.include? checkstring %>
<div class="row">
<%= t(lang[0..-4]) %> - <%= lang[-2, 2] %> <br />
<%= f.text_area lang, rows: "3" %>
</div>
<% end %>
<% end %>
<% end %>
Not efficient, functional.

No route matches [POST] "/basic_pages/basic_pages/home"

So I would simply like to post to my root. My problem is that if I try to do it the second time (first time works fine) I am posting /basic_pages/basic_pages/home instead of /basic_pages/home. Why does that happen and how do I fix this?
home.html.erb:
<h1>BasicPages#home</h1>
<p>Find me in app/views/basic_pages/home.html.erb</p>
<%= form_tag ('basic_pages/home') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>
routes.rb
Rails.application.routes.draw do
root 'basic_pages#home'
post 'basic_pages/home'
get 'about' => 'basic_pages#about'
end
Hope this provides all the information necessary.
Instead of using form_tag ('basic_pages/home') use form_tag ('/basic_pages/home'). When you have posted the url is getting changed to /basic_pages/home, and then the form is again posting to /basic_pages/basic_pages/home, because of the relative path given to the form.
Try this
Rails.application.routes.draw do
post '/' => "basic_pages#home", as: "root"
get 'about' => 'basic_pages#about'
end
home.html.erb:
<%= form_tag ('/') do %>
<%= text_field_tag :my_input %>
<%= submit_tag "Send input" %>
<% end %>

Select_tag wont dispay anything

I have the following code
<%= form_tag('/update', method: :post) do %>
<%= select_tag :role, UserSomething.roles.keys.map {|role| [role.titleize,role]} %>
Role is an enum which text values have to be displayed in the drop down menu, and on form submit, i have to send the index of selected enum to some controller. I don't know how to set select_tag propertly.
I would use the helper options_for_select to map the array you get from UserSomething.roles.keys.map to a list of options for the select. I don't think out the select_tag method handles an array of the box, it needs a list of option tags. See the docs here.
<%= form_tag('/update', method: :post) do %>
<%= select_tag :role, options_for_select(UserSomething.roles.keys.map {|role| [role.titleize,role]}) %>
<% end %>

Rails 4, Draper: authenticated user and views

<% if user_signed_in? %>
<!-- lots of html/erb -->
<% end %>
This view pattern seems to not separate concerns.
I wrap several views in my app with logic demanding the user is signed in and would instead like to separate concerns and put the <% if user_signed_in? %> logic where it belongs...this seems like a decorator thing to me (hence the Draper tag).
What is best practice here?
Not sure understood your question, but try to answer.
At first to separate logic you dont need to use decorators in front of all, they serves for a little another thing.
To separate logic you can use simple partials depending on current user state, for ex:
<% if user_signed_in? %>
<%= render 'file_with_html_for_signed_user' %>
<% else %>
<%= render 'file_with_html_for_non_signed_user' %>
<% end %>
You can declare this statement in your layouts/application.html.erb

Rails - loops and routes, and getting . instead of /

I have three controllers, books, users and contributions. In a view in the books controller I have the following snippet -
<%= link_to contrib.user.username, show_users_path(contrib.user_id) %>
Which returns /users.n rather than /users/n
<%= link_to contrib.user.username, "users/#{contrib.user_id}" %>
does the same thing.
I've read SO questions about this problem with nested resources, and with custom routes, but I've simply got my routes set up as resources, as follows -
devise_for :users, :controllers => { registrations: 'registrations' }
resources :blogs
resources :books
resources :users
resources :contributions
In users/index I have the snippet
<%= link_to user.username, "users/#{user.id}" %>
which works fine. What is going on?
.....
The problem was I'm using an each loop. The whole snippet goes
<% if controller.controller_name == "books" %>
<p><strong>by <%=link_to book.user.username, book.user %></strong></p>
<% book.contributions.each do |contrib| %>
<p><%= link_to contrib.user.username, user_path(contrib.user) %></p>
<%end%>
<% end %>
I changed the fourth line to this -
<p><%= link_to contrib.user.username, user_path(book.contrib.user) %></p>
Which works. Can anyone explain why?
It's because the default path for the show action is object_path(object_id) - there i no default route called show_object_path - I was confusing the path with action.