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.
Related
Pretty new to Ruby on Rails and reached my first road block I can't seem to figure out after some researching.
On my view, I am generating a button for each instance of "Newbie"
I would like to send the 'zendeskid' of each to my controller "giveticket"
So far, I have:
<div class="container">
<div class="containertop"></div>
<p><%= newby.name %></p>
<%= button_to "Give Ticket!", {:controller => "giveticket",:action => "new", :newby.zendeskid => #newby.zendeskid} %>
</div>
</div>
<% end %>
however, I am getting "undefined method `zendeskid' for :newby:Symbol"
Could someone please point me in the right direction - a bit stuck at the moment! Thank you in advance.
To send any parameters to the controller action you can just send it with the action name.
Named routing is more preferable and recommended to use in rails. So you can write as,.
<%= button_to "Give Ticket!", new_giveticket_path(zendeskid: newby.zendeskid) %>
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
EDIT: SOLVED, view the solution in Brian Kunzig's answer+comments.
I've decided to try out Ruby on Rails and have been constantly running into this problem. I was searching around for problems like this but none of the solutions have done the trick for me. It seems that I constantly write a part of code that just isn't correct. So here are some code snippets:
(ignore what the code would be for, it's just to try stuff out)
My controller:
class AuthsController < ApplicationController
def index
#auths=Auth.all
end
def new
#auth=Auth.new
end
def show
#auth=Auth.find(params[:id])
end
end
My index.html.erb:
<div style="margin: 0 auto; width:50%; text-align: center">
<h1>Please authorise your use of this webpage and its database(s).</h1>
<%= form_for :auth, url: auths_path do |f| %>
<% if #auth.errors.any? %> ==> RAILS REPORTS ERROR IN CODE HERE
<div id="error_explanation">
<h2>
<%= pluralize(#auth.errors.count, "error") %> prohibited
this authentification from being saved:
</h2>
<ul>
<% #auth.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :username %><br>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %><br>
<%= f.text_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
So, bottom line. Rails is saying that I cannot use #auth in the form_for block, for example. Or anywhere else for that matter.. It always says that it belongs to NilClass or something like that. It obviously wants me to instantiate it somehow, but isn't it enough to make the method new and put in the line: #auth=Auth.new ?
I'm just confused with this situation because I can't figure out how it's supposed to go. Thanks a lot !
P.S. I'm using <%= form_for :auth, url: auths_path do |f| %> because it won't accept #auth, that's what the error in the next line is. I have seen solutions to instantiate it "on the go" outside of the controller but I want to do it the way it's supposed to be done.
You should be putting this form in the new.html.erb file and not the index. The index is for listing entries while the new and create actions handle POST requests. You're getting an error because you're trying to list all Auth's when none have been created. Also, you're sending a form via a GET request if you're using standard rails routing. Use resourceful routing and put this form in your new view for that controller and it should work.
Routes file should be:
resources :auths
This will provide all the necessary routing automagically. If you type rake routes after modifying this you will see the newly generated urls and the helpers to them. You will notice it affords the create and update actions a POST/PUT request while others are GET.
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 %>
I am trying to use a basic current_page method to define whether a navigation link should be highlighted as the current page or not.
I am not receiving any errors at the moment but am clearly not defining things properly as it's not using my CSS correctly.
In my PagesController I have the following:
def current_page
current_page = (path)
end
and I am using an if and else statement on my pages view to try and define which CSS line to use which looks like this:
<div id="nav">
<ul>
<li>
<% if "current_page" %>
About
<% else %>
About
<% end %>
</li>
</ul>
</div>
I have read quite a few forums on this but still can't seem to get it right.
Mhh you got an error in your code. With if "current_page" you will get always true, and only the first link will get rendered. You should use if current_page instead.
But rails has a build in helper, called current_page? for exactly this purpose:
Just do it like this:
<% if current_page?(path) %>
About
<% else %>
About
<% end %>
See here for more information on current_page?
The klump's answer follows your requirement, but I dare to propose a different approach:
<div id="nav">
<ul>
<li>
<%= link_to_unless_current 'About', about_path %>
</li>
</ul>
</div>
This way your About will appear as a text if current page is about_path and as a link on any other.
About