I'm having trouble passing locals to a partial shared by three different views, each related to different actions in different controllers. The partial and the locals passed to it work without a problem when working with html requests, but I cannot get them to work when issuing xhr requests. Let me show you my code to explain myself better.
So, I have this partial
# app/views/shared/_vote_form.html.erb
<div id="vote_form">
<% if post.votes.find_by(user_id: current_user.id).nil? %>
<%= render partial: "votes/vote", locals: { postv: post,
vote: post.votes.build } %>
<% else %>
<%= render partial: "votes/unvote", locals: { postu: post,
vote: post.votes.find_by(user_id: current_user.id) } %>
<% end %>
</div>
As you can see, this partial renders one of two partials depending on the outcome of an if
statement:
# app/views/votes/_vote.html.erb
<%= form_for([postv, vote], remote: true) do |f| %>
<%= hidden_field_tag 'vote[vote]', 1 %>
<%= f.submit "Vote", class: "btn" %>
<% end %>
# app/views/votes/_unvote.html.erb
<%= form_for([postu, vote], html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unvote", class: "btn btn-primary" %>
<% end %>
As I mentioned, this partials are shared by three different views associated to different actions in different controllers.
# app/views/posts/show.html.erb
<div class="post-vote-form">
<%= render partial: "shared/vote_form", locals: { post: @post } %>
</div>
Which is associated to the following action in the following controller:
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
end
end
Another view
# app/views/users/feed.html.erb
<% @feed_items.each do |f| %>
<div class="vote-button">
<%= render partial: "shared/vote_form", locals: { post: f } %>
</div>
<% end %>
Associated to the following controller#action
class UsersController < ApplicationController
def feed
@user = User.find_by(id: current_user.id)
@feed_items = @user.feed
end
end
And finally
# app/views/categories/other.html.erb
<% @gal_items.each do |f| %>
...
<%= render partial: "shared/vote_form", locals: { post: f } %>
<% end %>
Associated to controller#action
class CategoriesController < ApplicationController
def other
@other = Category.find(7)
@gal_items = @other.posts
end
end
As you can see, the forms send an xhr request to create/destroy an instance of Vote (I have routes for Vote
nested in Post
. That's why the form_for
takes two arguments). These requests are handled by the following actions in the VotesController
class VotesController < ApplicationController
def create
@post = Post.find_by(id: params[:post_id])
@vote = @post.votes.build(vote_params)
@vote.user_id = current_user.id
@vote.save
respond_to do |format|
format.html { redirect_to @post }
format.js
end
end
def destroy
@vote.destroy
respond_to do |format|
format.html { redirect_to @post }
format.js
end
end
end
And these two js.erb
files come into play:
# app/views/votes/create.js.erb
$("#vote_form").html("<%= escape_javascript(render :partial => 'votes/unvote', locals: { postu: @post,
vote: @post.votes.find_by(user_id: current_user.id)}) %>");
And
# app/views/votes/destroy.js.erb
$("#vote_form").html("<%= escape_javascript(render :partial => 'votes/vote', locals: { postv: @post.each,
vote: @post.votes.build }) %>");
The way I am presenting these last two js.erb
files work for the view # app/views/posts/show.html.erb
as the values for the locals are taken directly from the VotesController
actions, but I have not been able to find a way to make it work for the other two views (which are @something.each do |f|
) that render these partials, as I cannot pass the appropriate values to the locals for the form_for
arguments to work. I have tried with a helper to pass values to the locals depending on the url, but without success. It seems obvious that I cannot get these js.erb
files to render the partials with appropriate values for the locals because I cannot retrieve the variables from their respective controllers.
So, bottomline, is there a way to make it work through these js.erb
files, or will I have to sort this out using pure JQuery? Has anyone faced something like this? I am sorry that I cannot make a question that requires a more specific answer. Hope you guys can help.
Did you try this?
class UsersController < ApplicationController
def feed
@post = Post.find(params[:id])
@user = User.find_by(id: current_user.id)
@feed_items = @user.feed
end
end
class CategoriesController < ApplicationController
def other
@post = Post.find(params[:id])
@other = Category.find(7)
@gal_items = @other.posts
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.