问题
This is likely an error due to my minimal understanding of Rails and how to use variables across models, so if there is more code needed to answer it or if my terminology is incorrect, let me know and I will gladly update the question.
I have a feed of posts that I want a user to be able to "like." While the following code allows likes to work on an individual post's page - site.com:3000/posts/*post.id*
- with the form data being passed of like[liked_post_id]:*post.id*
, when I try to submit a like on a profile - site.com:3000/users/*user.id*
- which contains a feed of posts, the form data being passed is like[liked_post_id]:
(blank value)
How can I pass the post's ID within a feed of posts to the liked_post_id
variable in _like.html.erb
?
I have noticed that the action of the like form is /likes
across the board. Would this will only work when you are on the page site.com:3000/posts/*post.id*
? I'm curious if I need to modify the it so that the action of the form is /posts/*post.id*/likes
when you are on the page site.com:3000/users/*user.id*
From my post view:
#views/posts/_post.html.erb:
...
<%= render 'posts/like_form' if signed_in? %>
...
Route to proper form:
#views/posts/_like_form.html.erb:
<div id="like_form">
<% if current_user.likes_this?(@post) %>
<%= render "posts/unlike" %>
<% else %>
<%= render "posts/like" %>
<% end %>
</div>
Like from:
#views/posts/_like.html.erb
<%= form_for Like.new, :remote => true do |f| %>
<%= f.hidden_field :liked_post_id, :value => @post.id %>
<%= f.submit "Like" %>
<% end %>
From profile (feed of posts):
#views/users/show.html.erb
...
<%= render @posts %>
...
Likes controller:
#controllers/likes_controller.rb
class LikesController < ApplicationController
before_filter :signed_in_user
def create
@post = Post.find(params[:like][:liked_post_id])
current_user.like!(@post)
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
end
...
User model:
#models/user.rb
...
def like!(post)
likes.create!(liked_post_id: post.id)
end
...
@frank-blizzard has pointed out that my form markup is an issue. On a post's page the generated markup is:
<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" value="73" />
While on the feed page:
<input id="like_liked_post_id" name="like[liked_post_id]" type="hidden" />
回答1:
You can do something like this:
<% form_for Like.new, :remote => true do |f| %>
<%= f.hidden_field :post_id, :value => @post.id %>
<%= f.submit %>
<% end %>
The current_user.likes.build(...)
part should get out of your view and inside your controller. You are using a current_user.like!
method so I guess you have implemented already some method in user model to accomplish this. If not build your like in the create
action of LikesController where you can access params[:like]
.
def create
@post = Post.find(params[:like][:post_id])
current_user.likes.build(@post)
# ...
end
EDiT
You might need to pass your @post variable correctly into your _like_form partials, like so:
#views/posts/_post.html.erb:
...
<% if signed_in? %>
<%= render 'posts/like_form', :post => @post %>
<% end %>
...
This will give you acceess to a post
variable inside the partial so you can prepopulate your forms value with its id. See this questions as well Pass a variable into a partial, rails 3? and make sure to read up on how to pass variables correctly to partials. you can debug your views using <%= debug <variablename> %>
来源:https://stackoverflow.com/questions/14802294/passing-an-instance-variable-into-a-form-rails