I have a Concert model and Comment model
The user directly write his comments in the concert show, but for any reasons he may want to edit it...
What is the easiest way to do this? I don't want to go to the edit view, but I'd rather prefer to edit it in the show page...
How or can I use the same form that I have in that show?
I was thinking of a modal but I am not sure it's friendy user...
Any help would be very welcome
concerts/show.html.erb
<h1><%= #concert.kountry%> - <%= #concert.city %> - <%= #concert.venue %> </h1>
<% #concert.comments.each do |c| %>
<%= c.user.email %>
<%= l(c.created_at, format: '%d/%m/%y - %H:%M:%S') %> - <%= link_to "Modifier", edit_concert_comment_path(c) %>
<%= simple_format(c.content) %>
<% end %>
<%= simple_form_for([#concert, #comment]) do |f| %>
<%= f.input :content, as: :text, placeholder: "your comment", label: false, input_html: { rows: 3 } %>
<%= f.submit "Send", class: "btn btn-success" %>
<% end %>
You can try using the Best In Place gem, hasn't been updated in a while but should still work.
modify form
<% #concert.comments.each do |comment| %>
<%= comment.user.email %>
<%= l(comment.created_at, format: '%d/%m/%y - %H:%M:%S') %>
<%= best_in_place(comment, :content, as: :textarea) %>
<% end %>
then modify controller
def update
#comment = Comment.find params[:id]
respond_to do |format|
if #comment.update(update_comment_params)
format.html { redirect_to(#comment, notice: 'Comment was successfully updated.') }
format.json { respond_with_bip(#comment) }
else
format.html { render action: "edit" }
format.json { respond_with_bip(#comment) }
end
end
end
Related
I've had an existing comment form on a resource show.html.erb that I want to be added to the show page without having to reload the entire page. I understand that with ajax via the 'form_with' form, this should be entirely possible.
My issue is that whilst the form does create a new comment, it only appears after I refresh the page.
I also get the following error.
ArgumentError (too few arguments):
app/controllers/comments_controller.rb:18:in 'format'
app/controllers/comments_controller.rb:18:in 'create'
comments_controller
class CommentsController < ApplicationController
before_action :load_commentable
before_action :authenticate_user!
before_action :comment_auth, only: [:edit, :update, :destroy]
...
def create
#comment = #commentable.comments.new(allowed_params)
#comment.user_id=current_user.id if current_user
if #comment.save
format.html { redirect_to #commentable, notice: "Comment added." }
format.js
else
render 'edit'
end
...
views/comments/create.js.coffee
comment = document.getElementById("comment")
comment.innerHTML = "<%= j render(#comment) %>"
views/comments/_form.html.erb
<%= form_with(model: [#commentable, #comment]) do |f| %>
<% if #comment.errors.any? %>
<div class="error_messages">
<h2>Please correct the following errors.</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<strong><%= f.label :name, :class=>'form-label' %></strong><br>
<%= f.text_field :name, value: current_user.fname, readonly: true, :class=>'form-control' %>
</p>
<p>
<strong><%= f.label :title, :class=>'form-label' %></strong><br>
<%= f.text_field :title, :class=>'form-control' %>
</p>
<p>
<strong><%= f.label :review, :class=>'form-label' %></strong><br>
<%= f.text_area :body, :class=>'form-control', rows: 5 %>
</p>
<p>
<%= f.submit :class=>'btn btn-success' %>
</p>
<% end %>
For me, I needed
<%= form_with(model: #message, method: :post, local: true) do |f| %>
local: true was necessary. All other combos didn't work remote: true, remote: false, local: false
as you know, form_with it's a remote form, you should not use redirect_to there. Please remove your block
if #comment.save
format.html { redirect_to #commentable, notice: "Comment added." }
format.js
else
render 'edit'
end
replace it to
if #comment.save
#status = "success"
end
And you can add a notice message to your views/comments/create.js.erb (it's not .js.coffee) with condition of #status (failed/success).
Hope this helps.
I have a relationship model that follow users back n forth
now it works very well when I use it in users#show in my view just like this screenshot
now this works good but what I have is a list of users.. and with that list I want to render similar type of follow form next to every item in list
here is the screenshot for reference
but of course this doesn't work because in my relationship controller I am finding user/amitian to follow with their id in params[:id] as in show page i have it but in my list page i don't
here is the code in controller for reference
def create
#amitian = Amitian.find(params[:followed_id])
current_amitian.follow(#amitian)
respond_to do |format|
format.html { redirect_to #amitian }
format.js
end
end
def destroy
#amitian = Relationship.find(params[:id]).followed
current_amitian.unfollow(#amitian)
respond_to do |format|
format.html { redirect_to #amitian }
format.js
end
end
is there any solution for this so that I can render a follow_form in my listpage
it doesn't matter even if i have to render a new follow_form.. it just i want follow mechanism to work even from list page rather than only from show page of every user.
here is the show form in my show page..
show_form
<%if amitian_signed_in?%>
<% if #amitian != current_amitian %>
<div id="follow_form">
<% if current_amitian.following?(#amitian) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
<%end%>
follow_form
<%if amitian_signed_in?%>
<%= form_for(current_amitian.active_relationships.build , remote: true ) do |f| %>
<div><%= hidden_field_tag :followed_id, #amitian.id %></div>
<%= f.submit "Follow", class: "btn btn-primary form-control" %>
<% end %>
<%end%>
unfollow form
<%if amitian_signed_in?%>
<%= form_for(current_amitian.active_relationships.find_by(followed_id: #amitian.id), remote: true,
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-danger form-control" %>
<% end %>
<%end %>
For my application, I have projects where users can make comment (class Newcomments) postings. Right now it posts great but on page refresh. I am trying to use AJAX/JQUERY so that it will post with no page refresh. I am following this railscasts tutorial.
So right now, the posts are made to my database and the page does not refresh. But when I refresh, the posts shows up. The page that I render my comments for a specific project is on the projects/_comments.html.erb.
Question: How would I adjust it so that it the new comment made renders?
newcomments_controller.rb
def create
#newcomment = #commentable.newcomments.new(params[:newcomment])
if #newcomment.save
respond_to do |format|
format.html { redirect_to comments_project_path(#commentable) }
format.js
end
else
render :new
end
end
view/newcomments/_form.html.erb
<span class="comment">
<%= form_for [#commentable, #newcomment], remote: true do |f| %>
<%= f.text_area :content, rows: 3, :class => "span8" %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit "Add Comment", :class => "btn btn-header" %>
<% end %>
</span>
view/newcomments/create.js.erb
$('#newcomment').append('<%= j render(#newcomments) %>');
projects_controller.rb
def comments
#commentable = #project
#newcomments = #commentable.newcomments.newest.page(params[:comments_page]).per_page(10)
#newcomment = Newcomment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: #project.comments }
end
end
projects/comments.html.erb
<%= render 'comments' %>
projects/_comments.html.erb
<%= render #newcomments %>
view/newcomments/_newcomment.html.erb
<div class="comments">
<%= link_to newcomment.user.name %></strong>
Posted <%= time_ago_in_words(newcomment.created_at) %> ago
<%= newcomment.content %>
</div>
<span class="comment">
<%= form_for [#commentable, #newcomment] do |f| %>
<div class="field">
<%= f.text_area :content, rows: 3, :class => "span8" %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.submit "Add Comment", :class => "btn btn-header" %>
</div>
<% end %>
<% unless newcomment.newcomments.empty? %>
<%= render #newcomments %>
<% end %>
</span>
Try binding AJAX actions as described here:
http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
Also, consider returning a render comments partial html instead of json comment object, to do so you need to tell in form after :remote directive that :'data-type'=>'html', so that returned html will hit function binded on AJAX success, and then swap html of container div with, for example, jQuery
I am trying to render a partial through ajax when a user clicks on button destroying a relationship between two products. The destroy action works, but I can not get the rendering of the partial to work.
The partial is rendered on the edit page for a product:
edit.html.erb
<div id="relationship_form">
<%= render "relationships", product: #product %>
</div>
_relationships.html.erb
<%= render "unrelate", relationship: relationship %>
_unrelate.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>
relationships_controller.html.erb
def destroy
...
respond_to do |format|
format.html { redirect_to edit_product_path }
format.js
end
end
destroy.js.erb
$("#relationship_form").html("<%= escape_javascript(render :partial => 'products/relationships', :locals => {:product => #product}) %>");
I have checked that the ajax call goes through to the destroy.js.erb, by inserting an alert, so there must be an error in the javascript?
Update:
I got it to work by moving the form in the _unrelate partial to the _relationship partial. Still not really clear why it made it work.
_relationships.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>
Here's the scoop: I've created a test app that allows users to create ideas and then add "bubbles" to these ideas. Currently, a bubble is just text. I've successfully linked bubbles to ideas. Furthermore, when a user goes to view an idea it lists all of the bubbles attached to that idea. The user can even delete the bubble for a given idea.
My problem lies in editing bubbles. When a user views an idea, he sees the idea's content as well as any bubbles for that idea. As a result, I've set all my bubble controls (editing and deleting) inside the ideas "show" view. My code for editing a bubble for an idea is <%= link_to 'Edit Bubble', edit_idea_bubble_path %>. I ran rake routes to find the correct path for editing bubbles and that is what was listed.
Here's my error: No route matches {:action=>"edit", :controller=>"bubbles"}
In my bubbles controller I have:
def edit
#idea = Idea.find(params[:idea_id])
#bubble = #idea.bubbles.find(params[:id])
end
def update
#idea = Idea.find(params[:idea_id])
#bubble = #idea.bubbles.find(params[:id])
respond_to do |format|
if #bubble.update_attributes(params[:bubble])
format.html { redirect_to(#bubble,
:notice => 'Bubble was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "Edit" }
format.xml { render :xml => #bubble.errors,
:status => :unprocessable_entity }
end
end
end
To go a step further, I have the following in my routes.rb file
resources :ideas do
resources :bubbles
end
So far everything seems to function except when I try to edit a bubble.
I'd love some guidance.
Thanks!
Here's my show.html.erb file for Ideas:
<h2>Bubbles</h2>
<% #idea.bubbles.each do |bubble| %>
<p>
<b>Bubble:</b>
<%= bubble.description %>
</p>
<p>
<%= link_to 'Edit Bubble', edit_idea_bubble_path (#idea) %>
</p>
<tb />
<p>
<%= link_to 'Delete Bubble', [bubble.idea, bubble],
:confirm => 'Are you sure you want to delete this bubble?',
:method => :delete %>
</p>
<% end %>
<h2>Add a bubble:</h2>
<%= form_for([#idea, #idea.bubbles.build]) do |f| %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %> </div><% end %>
Following edit_idea_bubble_path (#idea), here is the edit.html.erb file for Bubbles:
<%= render 'form' %>
<%= link_to 'Back to Ideas', ideas_path %>
And finally, my _form.html.erb file for Bubbles: this is where the problem lies, I believe
<% form_for([#idea, #idea.bubbles.build]) do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
First build the route in your show.html.erb file
<%= link_to 'Edit Bubble', edit_idea_bubble_path(#idea, bubble) %>
Then, your controller should have 2 objects for both #idea and #bubble
when action is new
#idea = Idea.find_by_id(:params[:idea_id])
#bubble = #idea.bubbles.build
when action is edit
#idea = Idea.find_by_id(:params[:idea_id])
#bubble = #idea.bubbles.find_by_id(:params[:bubble_id])
In your _form.html.erb
<% form_for([#idea, #bubble]) do |f| %>
You have to provide the idea and the bubble objects:
<%= link_to 'Edit Bubble', edit_idea_bubble_path(#idea,#bubble) %>
Update:
Edit the _form.html.erb file
<% form_for([#idea, #bubble]) do |f| %>