I added a link to the Photos page so that below each picture it would link to the users page.
The issue is I am getting redirected to /photos/1 when I should be directed to /users/1
Photos controller:
def show
#photos = Photo.find(username: params[:id])
end
View:
<% #photos.each do |photo| %>
<%= link_to photo.user, photo %>
<%=image_tag photo.image_url(:thumb) %>
<%= button_to "remove", photo, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
As noted, using <%= link_to photo.user.username, photo.user %> should give you what you're looking for.
Related
Hi I have a form in rails and this is the code
<%= form_tag getjson_products_path do %>
<% #products.each do |product| %>
<%= check_box_tag "product_ids[]",product.id , false%>
<%= product.name %>
<%= product.category %>
<%= product.price %>
<%= link_to 'Show', product %>
<%= link_to 'Edit', edit_product_path(product) %>
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
<br>
<% end %>
<%= submit_tag "submit" %>
<br>
<% end %>
and I have written one method in products controller
def getjson
#stuff to do
redirect_to root_path
end
and this is my routes file
resources :products do
collection do
get 'getjson'
end
end
I want is to get json value of selected products , but whenever I click submit it says routes error what I have to do and how to get json value for selected records?
In controller change like this
def getjson
if params[:product_ids]
#products = Product.find(params[:product_ids])
render json: #products
end
end
thats all it will works
I'm trying to add some social functionality to my app, and following RailsCast #163 about self-referential association, but I have a problem with deleting friendship.
On user#show page I have 2 columns: with #users and #friends. The show method from UsersController is:
def show
#user = User.find(params[:id])
#users = User.all
#friends = #user.friends
end
Also I'm using <%= render #users %> and <%= render #friends %> partials, both of them renders _user.html.erb from users folder, which is the following:
<tr>
<td>
<%= gravatar_for user, size: 30 %>
</td>
<td>
<%= user.name %>
</td>
<td>
<% if current_user.friends.exists?(user) %>
<%= link_to 'Remove friend', friendship, :method => :delete %>
<% else %>
<%= link_to 'Add friend', friendships_path(:friend_id => user), method: :post %>
<% end %>
</td>
</tr>
Everything is ok with models and controllers, as I've checked everything a hundred times. But when I try to open a page I receive an error undefined local variable or method friendship from this line <%= link_to 'Remove friend', friendship, :method => :delete %>.
Like the error says, friendship is not defined. You need to pass instead of friendship, the user you want to delete. For example:
<%= link_to 'Remove friend', user, :method => :delete %>
or
<%= link_to 'Remove friend', friendship_path(user), :method => :delete %>
Hope this helps!
It looks awful, but seems that current_user.friendships.find_all_by_friend_id(user.id).first instead of friendship solved my problem. But I'm sure that there is more simple solution.
if you have an instance variable defined in your controller (#friendship or #user) use that opposed to user or friendship for the route.
<%= link_to 'Remove friend', #user, :method => :delete %>
I am working a on rails 3 project and am trying to render a partial on the user profile page. Rendering works fine for everything except photo. When I try to access a user profile page I get the following error
undefined methodmodel_name' for NilClass:Class`
user show.html.erb
def show
#user= User.find(params[:id])
#photo = Photo.find_by_id(params[:id])
end
In the profile show.html.erb
<%= content_tag :h2, "My photos" %>
<% photo = #user.photos %>
<% if photo.blank? %>
<%= link_to 'Create photo', new_photo_path %>
<% else %>
<%= render :partial => 'photos/photo', :locals => {:photo => #photo} %>
<% end %>
Here is the partial that it is rendering
<%= div_for photo do %>
<table>
<tr>
<th>Title</th>
<th>Date</th>
<th>Extra</th>
</tr>
<tr>
<td><%= photo.title %></td>
<td><%= photo.date %></td>
<td><%= photo.extra %></td>
<td><%= link_to 'View photos', photo %></td>
</tr>
</table>
<% end %>
Any ideas or suggestions on how I can get rid of this error? Thanks in advance
The error is thrown because in :locals => {:photo => #photo}, #photo is null
Note that you are sending the list #user.photos, not a single image.
Here #photo = Photo.find_by_id(params[:id]) you are getting the photo by id, photo_id. I believe you want to find the user's photo
#photos = #user.photos
#for only one photo, also you need to make sure its not null
#photo = #user.photos.first
There isn't photo with this id in database #photo = Photo.find_by_id(params[:id])
And, therefore, you sending nil object to partial. If you want to show all user photos, try this.
<%= content_tag :h2, "My photos" %>
<% photos = #user.photos %>
<% if photos.empty? %>
<%= link_to 'Create photo', new_photo_path %>
<% else %>
<% photos.each do |photo| %>
<%= render :partial => 'photos/photo', :locals => {:photo => photo} %>
<% end %>
<% end %>
When using the div_for note is nil so I would try accessing it by iterating through the code Like I've done below. Then photos should be accessible now.
<% #photos.each do |photo| %>
<%= div_for photo do %>
Code here
<% end %>
<% end %>
As I always type I am new to rails and programming in general so go easy. Thanks in advance.
I have successfully followed the initial tutorial from Ryan Bates on how to build a weblog in 15 minutes. If you don't know this tutorial takes you through creating posts and allowing for comments on those post. It even introduces AJAX through the creating and displaying comments on the posts show.html.erb page. All works great.
Here's the hiccup, when Ryan takes you though this tutorial he clears out the comments_controller and only shows the code for creating comments. I am trying to add back the ability to edit and destroy comments. Can't seem to get it to work, keeps deleting the actual post not the comment (log shows that I keep sending DELETE request to PostsController). Here is my code:
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create!(params[:comment])
respond_to do |format|
format.html { redirect_to #post }
format.js
end
end
def destroy
#comment = Comment.find(params[:id])
#comment.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
/views/posts/show.html.erb
<%= render :partial => #post %>
<p>
<%= link_to 'Edit', edit_post_path (#post) %> |
<%= link_to 'Destroy', #post, :method => :delete, :confirm => "Are you sure?" %> |
<%= link_to 'See All Posts', posts_path %>
</p>
<h2>Comments</h2>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<% remote_form_for [#post, Comment.new] do |f| %>
<p>
<%= f.label :body, "New Comment" %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Add Comment" %></p>
<% end %>
/views/comments/_comment.html.erb
<% div_for comment do %>
<p>
<strong>Posted <%= time_ago_in_words(comment.created_at) %> ago
</strong><br/>
<%= h(comment.body) %><br/>
<%= link_to 'Destroy', #comments, :method => :delete, :confirm => "Are you sure?" %>
</p>
<% end %>
routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :posts, :has_many => :comments
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
meagar is on the right path, but since this is a nested route you have to do:
<%= link_to 'Destroy', [#post, comment], ... %>
So, you are passing the comment and the post and letting rails figure out the route based on your definitions.
In _comments.html.erb, change your link_to to
<%= link_to 'Destroy', comment, ... %>
IE, pass it the comment itself, not the entire #comments array.
I am having issues using the :collection command for a partial within a form I am creating in rails. I would ideally like to use the :collection command, so I can easily manipulate this section in my .rjs templates (the form will submit and reload the form when the check box is changed, it's a to-do list).
This code works:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<% for #list in #lists %>
<%= render :partial => #list, :locals => {:f =>f, :complete => FALSE } %>
<% end %>
</ul>
<% end %>
with the partial:
<% if #list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h #list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
This code does not work, but I would like it to use this form:
<% form_for "list[]", :url => {:action => "checkbox_update"} do |f| %>
<ul id="lists_not_completed">
<%= render :partial => 'list', :collection => #lists, :locals => {:f =>f, :complete => FALSE } %>
</ul>
<% end %>
with the non-working partial:
<% if list.completed == complete %>
<li><%= f.check_box :completed %>
<%=h list.name %>
<%= link_to 'Show', list %>
<%= link_to 'Edit', edit_list_path(list) %>
<%= link_to 'Destroy', list, :confirm => 'Are you sure?', :method => :delete %></li>
<% end %>
I get the error:
object[] naming but object param and #object var don't exist or don't respond to to_param: nil. It is referring to this line: <li><%= f.check_box :completed %>. I'm not sure if why this doesn't work and have tried many, many different variations, but I can't get it working. Is the form preventing me from doing this? The form_for code is straight from the Rails Way book for listing multiple objects from one model in a form.
Any help on this would be greatly appreciated.
I think that the problem is you've not got #list defined anywhere when you're using the render :partial with a :collection.
The system is looking for #list to match the list[] when you call f.check_box
you could set #list = list in your partial to get around that. I suppose.
Tim's answer is correct, but I'd probably avoid extracting the partial within the form_for loop altogether. I suppose it's a matter of style, but I think the confusion here isn't really worth the cleanup that the partial represents in this case. I'd probably write a partial that included the whole form.