Rails 4: Custom Action To Update Only One Param With Ajax
In my Rails 4 app, I have a Calendar and a Post models, using shallow routes: resources :calendars do resources :posts, shallow: true end A calendar has_many post and a post bel
Solution 1:
I come up with 2 ideas to handle this situation:
First way: Pass another param in when you update the post from calendarshow.html.erb
, so the link_to
will be:
<%= link_to post_path(:id => post.id, "post[approval]" => "remove", approval_update:true), remote:true, :method => :patchdo %>
Then in you Post controller on update
action, you can filter this request by approval_update
param:
if params["approval_update"]
respond_to do |format|
format.js { render :action => "update_post_approval" }
endelse
respond_to do |format|
...
endend
Now when you update Post from Calendar show.html.erb, Post controller will load RAILS_ROOT/app/views/posts/update_post_approval.js.erb
instead of update.js.erb
Second way: create another route for update the approval attribute of Post, which mean you will also create another action in Post controller to handle the case of AJAX update in Calendar page.
Solution 2:
I think you should be able to do this with a condition in the js.erb:
<% if@post.approval === 'ok' %>
$('td.post_approval_section').replaceWith("<%= j render(:partial => 'calendar/approval') %>");
<% else %>
// you current update post code
<% end %>
along with moving the code to _approval.html.erb
<% if post.approval == "ok" %>
<span class="ok_green">
<% else %>
<span class="approval_blue" %>
<% end %>
<%= link_to post_path(:id => post.id, "post[approval]" => "ok"), remote: true, :method => :patch do %>
<span class="glyphicon glyphicon-ok" data-toggle="tooltip" data-placement="left" title="Approve Post"></span>
<% end %>
</span><br/>
<% if post.approval == "edit" %>
<span class="edit_yellow">
<% else %>
<span class="approval_blue" %>
<% end %>
<%= link_to post_path(:id => post.id, "post[approval]" => "edit"), remote: true, :method => :patch do %>
<span class="glyphicon glyphicon-repeat" data-toggle="tooltip" data-placement="left" title="Require Edits"></span>
<% end %>
</span><br/>
<% if post.approval == "remove" %>
<span class="remove_red">
<% else %>
<span class="approval_blue" %>
<% end %>
<%= link_to post_path(:id => post.id, "post[approval]" => "remove"), remote: true, :method => :patch do %>
<span class="glyphicon glyphicon-remove" data-toggle="tooltip" data-placement="left" title="To Be Deleted"></span>
<% end %>
</span>
Hope that helps.
Post a Comment for "Rails 4: Custom Action To Update Only One Param With Ajax"