I have a _follow_form partial :
<% unless current_user == #player %>
<div id="follow_form">
<% if current_user.following?(#player) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
My _follow partial looks like this :
<%= form_for current_user.relationships.build(:followed_id => #player.id),
:remote => true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
My create.js.erb file :
$("#follow_form").update("<%= escape_javascript(render('players/follow')) %>")
But nothing happens when I create a Relationship. I mean the relation is created, but the partial doesn't update.
When I try with :
$("#follow_form").append("foobar");
It works.
How could I update #follow_form ?
Thanks for your help.
PS: I searched for a solution before posting, but everything I tried failed.
UPDATE
With :
$("#follow_form").html("<%= escape_javascript(render('players/unfollow')).html_safe %>")
Nothing happens, the partial isn't replaced but an error occurs :
NoMethodError in Relationships#create
Showing C:/Ruby/ostriker/app/views/players/_unfollow.html.erb where line #1 raised:
undefined method `model_name' for NilClass:Class
My _unfollow partial :
<%= form_for current_user.relationships.find_by_followed_id(#player),
:html => { :method => :delete },
:remote => true do |f| %>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
$("#follow_form").html("<%= escape_javascript(render('players/follow')).html_safe %>")
Related
I have Post with nested elements who also have different nested form (text for this example) according to a :cat variable.
To create the texts I'm using a post method create_element with a variable (cat = 1). And I would like to call an AJAX method to render the new text created into my post form.
My problem: I don't know how to call the text nested_form created directly into my AJAX method.
My code :
Post form (posts/new):
<%= simple_form_for #post do |f| %>
<div class="elements">
<%= f.fields_for :elements do |element| %>
<%= render 'posts/element_field', f: element %>
<% end %>
</div>
<div class="btn_add_element_area">
<a href="/posts/<%=#post.id%>/create_element?cat=1" data-method="patch" data-remote="true">
<i class="fa fa-font"></i>
</a>
</div>
<%= f.submit "Publish", class: "btn" %>
<% end %>
Post form (posts/_element_field):
<div class="element_form" id="element_form<%= f.object.id %>">
<div class="undisplay">
<%= f.number_field :cat %>
</div>
<%- if f.object.cat == 1 %>
<%= f.fields_for :post_texts do |text| %>
<%= render 'posts/elements/text_field', f: text %>
<% end %>
<% end %>
</div>
Post form (posts/_text_field):
<%= f.text_area :content %>
Post controller (create_element) :
def create_element
if params[:cat] == "1"
#element = Element.create(post: #post, cat: 1)
Text.create(element: #element)
end
respond_to do |format|
format.js {render 'elements/create'}
end
end
And the AJAX method (elements/create) :
$('.elements').append("<%= j render partial: 'posts/element_field', locals: {f: #element} %>");
Error = undefined method `object'
The problem is that in the AJAX file I call the #element created for my f variable instead of calling the form of this #element.
I also tried something like that for the AJAX method, but still not working :
<% form_for(#element) do |elm| %>
$('.elements').append("<%= j render partial: 'posts/element_field', locals: {f: elm} %>");
<% end %>
Error = undefined method `element_path'
Or
<%= simple_form_for(#post) do |p| %>
<%= p.fields_for :elements, #element do |elm| %>
$('.elements').append("<%= j render partial: 'posts/element_field', locals: {f: elm} %>");
<% end %>
<% end %>
No error but nothing happen
If you have any ideas to solve that, I would really appreciate !
PS: And ideas to improve the question title are welcome !!
In page1, I use code A+B+C
In page2, I use code B+C
So when I make a partial, I realy have no idea in how to deal with this.
For example, In a Post-Comment system. I want to show #comments in 2 different pages. In the comment index page,
We show the post it belongs to. And in the post show page, We only have to show the comments content.(Since there is no need to show the comment.post again)
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= comment.author %>
<%= comment.content %>
<% end %>
..
#Post Show Page
<% #comments.each do |comment| %>
<%= comment.author %>
<%= comment.content %>
<% end %>
So, how do I make a partial to reuse the code? Perhaps like this? But this there more elegant way of doing this?
#Comment Index Page
<% #comments.each do |comment| %>
<%= comment.post %>
<%= render comment %>
<% end %>
#Post Show Page
<% #comments.each do |comment| %>
<%= render comment %>
<% end %>
Updated:
I adopt the local variable approach, and update my code like:
# partial
<% if include_topic %>
<div class="Topic">
<h5><%= link_to "#{comment.topic.content}", comment.topic %></h5>
</div>
<% end %>
#Index
<%= render #comments, :locals => {:include_topic => true } %>
But I get undefined local variable or method `include_topic' for #<#
I just find nowhere to debug this issue
Your partial:
<%= comment.post if include_post %>
<%= comment.author %>
<%= comment.content %>
your code:
#index page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => true } %>
#show page
<%= render :partial => "partial_path", :collection => #comments, :as => :comment, :locals => {:include_post => false } %>
Syntax could be much shorter but it depends whether or not you stick to rails conventions see doc.
Sidenote: I don't like 1.8.7 syntax
In the partial,
<% comments.each do |comment| %>
<%= comment.post if params[:controller] == "comments" %>
<%= comment.author %>
<%= comment.content %>
<% end %>
and now render this partial in both comments/index and posts/show pages by specifying the comments as a local variable.
When i am in /edit action, i see edit button but the problem is if there is an error in form validation it renders action edit and i see create button. how can i fix it?
<%= form_for(#page) do |f| %>
<% if controller.action_name =="edit" %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>
<% if ["edit", "update"].include? params[:action] %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>
Better solution is to extract your form as a partial and send local variable with button name to it
your edit view:
<%= render :partial => "form", :locals => { :button_label => "Edit" } %>
your create view:
<%= render :partial => "form", :locals => { :button_label => "create" } %>
your _form partial:
<%= form_for #object ... do |f| %>
...
<%= f.submit button_label %>
<% end %>
UPD
I think #idlefingers solution the best for your issue
You could just use f.submit with no arguments. This will create names like "Update Page" and "Create Page". If you want to change the wording of these, they can be set in your locale. No conditionals, no messing about with action names. Simple.
Try to do this check:
<%= form_for(#page) do |f| %>
<% if controller.action_name =~ /update|edit/ %>
<%= f.submit "Update" %>
<% else %>
<%= f.submit "Create" %>
<% end %>
My solution:
<%= form_for(#page) do |f| %>
<%= f.submit(f.object.new_record? ? "Create" : "Update") -%>
<% end %>
I'm using basic scaffold structure. What I need, is to add 'moderate' action and view by changing published to true. In my idea, on moderate.html I should get the list of all unpublished entries with the ability to change and save their parameters.
Here are parts of my code:
#names_controller.rb
def moderate
#name = Name.find(:all, :conditions => {:published => false} )
respond_to do |format|
format.html
format.xml
end
end
#moderate.html.erb
<% form_for #name.each do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
Instead I'm getting this error:
NoMethodError in Names#moderate
Showing app/views/names/moderate.html.erb where line #1 raised:
undefined method `enumerable_enumerator_path' for #<ActionView::Base:0x1042c3e90>
Extracted source (around line #1)
So, can you help to newbie please?
ruby 1.8.7 (2009-06-12 patchlevel 174)
[universal-darwin10.0] Rails 2.3.5
If you want to update each name in a separate form, then all you need to do is move the loop above form_for:
<% #name.each do |n| %>
<% form_for n do |f| %>
<%= f.error_messages %>
<%= f.text_field :which %>
<%= f.text_field :what %>
<%= f.check_box :published %>
<%= f.submit %>
</p>
<% end %>
<% end %>
But if you'd like to do it all in one submit (a single form) then I guess you can't use form_for. I'd use form_tag to create a custom form to update multiple instances. This should work both for create and edit form:
<%= form_tag moderate_names_path do %>
<% #names.each do |name| %>
<fieldset>
<%= fields_for "name[#{name.id}]", name do |name_fields| %>
<p><%=name_fields.label(:this)%>: <br /><%= name_fields.text_field :this %></p>
<p><%=name_fields.label(:that)%>: <br /><%= name_fields.text_field :that %></p>
<p><%= name_fields.check_box :published %> <%=name_fields.label(:published)%></p>
<% end %>
</fieldset>
<br />
<% end %>
<%= submit_tag %>
<% end %>
NOTICE: I changed #name to #names in the second example
Ok, I am new to rails so I hope I explain this well. I seem to have a problem with a form collecting the proper information. Work in one spot and not another. Here is the situation.
I have created a blog with three tables, posts, comments, and votes. I have comments working correctly and votes working partially. Here is what I mean.
The /posts/show.html.erb file basically shows the post, the associated comments and it shows how many votes that post has. I am working the votes but using a form with a hidden field that delivers a value of '1' along with the post_id to the table. I am then using AJAX to return the #post.votes.count. This all works great.
I am trying to use this same vote/return mentioned above but on the /posts/index.html.erb page. In the index page I have the <% #post.each do |post| %> doing two things. First render :partial => post %> and then I have the same code that I am using on the show.html.erb page. The error that I am getting from the terminal is
ActiveRecord::RecordNotFound (Couldn't find Post without an ID):
app/controllers/votes_controller.rb:4:in `create'
I am guessing this has to do with it being part of the index record set which doesn't have a unique post param associated with the page. I am hoping there is a simple fix. Here is the code:
This is the /posts/show.html.erb code that is working correctly:
<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %>
</div>
<%= render :partial => #post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= #post.votes.count %> People liked this BattleCry.
</div>
</div>
<div id="votebutton">
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<div id="beltcommentsbottom">
</div><br/><br/>
<div id="belt">
<div id="belttext">
<p5>Add a Comment</p5>
<% remote_form_for [#post, Comment.new] do |f| %>
<p>
<%= f.text_area ( :body, :class => "commentarea") %>
</p>
<%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<div id="comments">
<%= render :partial => #post.comments %>
</div>
<br/>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is where it is in the index page:
<% #posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => #post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= post.votes.count %> People liked this BattleCry.
</div>
<%= link_to "Comments (#{post.comments.count})", post %>
</div>
<div id="votebutton">
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<br/>
<br/>
<br/>
<% end %>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is the votes_controller.rb
class VotesController < ApplicationController
def create
#post = Post.find(params[:post_id])
#vote = #post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to #post}
format.js
end
end
end
Here is the /votes/create.js
page.replace_html :votes, :partial => #vote
page[#vote].visual_effect :highlight
Lastly, here is the partial, /_vote.html.erb:
page.replace_html :votes, :partial => #vote
page[#vote].visual_effect :highlight
I hope this makes sense. Let me know if I need to clarify anything.
I imagine this is causing problems. You didn't post your posts controller, but does the index action set #post?:
<%= render :partial => #post %>
Looks like I just needed to change the following in the <% #posts.each do |post| %> section of the index.html.erb file.
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
to
<% remote_form_for [post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>