Link_to not working as expected - ruby-on-rails

I am trying to build a simple app using Ruby on Rails. Essentially, I have a route that maps to a controller, whose view looks like this:
<div class="wishlist-container">
<% #wishlists.each do |w| %>
<div class="wishlist-card">
<h4><%= w.title %></h4>
<%= link_to "View List", wishlist_path(w) %>
</div>
<% end %>
Everything works correctly except for the link. For whatever reason, the link links to "." instead of "/" where <id> is the id. For example, it should link to /wishlist/1 but instead goes to /wishlist.1.
What is happening? How can I solve this problem?

For using paths helpers in your code , you should specify the resources , not only get or post in your routes.rb
for example if you've
get 'wishlist/:id' It may not work .To make your path work you should specify
get 'wishlist/:id', to: 'wishlist#show', as: 'wishlist'
For more information read Ruby docs and This article

Related

Rails 4 Generating application wide dynamic nav-bar links

I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?
Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>

Rails link from database appends to current controller

I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."

undefined method `symbolize_keys' for "myparamsimtryingtopass":String

So im still very new to ruby on rails and what Im trying to do here is very simple. Im trying to create a facebook like app where if you click on the profile picture of a post it will direct you to the users profile page. I have just done something really similar in a online course but I cant seem to get this one to work in another view. Here is what I have that works in my header, navbar.
NAVBAR
<nav class="navbar navbar-default navbar-fixed-top">
......
<li><%= link_to "Show Profile" ,
user_profile_path(current_user.id, current_user.full_name) %></li>
....
</nav>
This code works and directs me to the corresponding users profile page.
Routes
Rails.application.routes.draw do
....
root 'statuses#index'
get '/:id/:full_name', to: 'profile#show' , as: :user_profile
.....
end
Problem View
<div class="page-header">
....
<% #statuses.each do |status| %>
<div class="row">
<div class="col-md-1">
<%= link_to image_tag(status.user.avatar.url(:thumb),
user_profile_path(status.user.id, status.user.full_name)) %>
//the above is what gives me the error in the title.
</div>
<% end %>
I have done my fair share of searching around and it seems that this error occurs if im passing in strings when it accepts hashkeys? Im not entirely sure. If there is a better way to do this that I should use please show me as I am very new and open to learning.
You have a wrong usage of a link_to helper. Instead of:
<%= link_to image_tag(status.user.avatar.url(:thumb), user_profile_path(status.user.id, status.user.full_name)) %>
^first argument ^second argument
Use:
<%= link_to user_profile_path(status.user.id, status.user.full_name) do %>
<%= image_tag(status.user.avatar.url(:thumb)) %>
<% end %>
As you can see, you pass _path helper as the second argument to the image_tag, this is wrong. The second argument to the image_tag should be a hash, thats why you have a undefined method 'symbolize_keys' for errors.

Rails: Having a small issue with link_to and external URLs

Pretty new to rails. I'm doing a project where a user submits a url and a title. The title is supposed to link to the url provided by the user. The url is stored as a param for link.
Here's the code from the index view:
<% #links.each do |link| %>
<%= link_to link.title, link.url %>
<%= link_to "comments", link %>
<% end %>
This works, for the most part.
The problem occurs if the submitted url doesn't begin with http://. As it is, it's pointing to http://localhost:3000/google.com and I get an error No route matches [GET] "/google.com"
How could I get around this? I tried changing it to:
<%= link_to link.title, "http://#{link.url}" %>
Which makes google.com work, but then http://google.com turns into http://http//google.com somehow.
I'm sure the fix will be a face palm moment!
Prepend url with protocol if it's absent:
module ApplicationHelper
def url_with_protocol(url)
/^http/i.match(url) ? url : "http://#{url}"
end
end
<%= link_to link.title, url_with_protocol(link.url) %>
answer derived from this SO question/answer
In your input field, you can do something like <input type="text" name="url" value="http://"> so that your url will always started with http://. User can also manually changed it to https if needed.
Also I may add a full_url method to the model that adds it if it's missing.

rails link_to with block, controller options and html post

I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>

Resources