Reputation: 53
I have big problem with my rails app. I create advanced blog for my schoolwork
i need to edit my comment, but my url return this '/posts/4%2F12/comments/4/edit' and not /posts/4/comments/12/edit
if I type the URL myself correctly I have access to the modification form that works
you can see my code here
show.html.haml
.article
.container
.row
.col-lg-12
.cover-image{:style => "background-image: url('#{@post.photo_url}');"}
%h1.mt-4= @post.title
#{I18n.l(@post.created_at)}
%hr/
%p.lead= @post.content.html_safe
%hr/
.card.my-4
%h5.card-header Ajouter un commentaire :
.card-body
= render 'comments/form', comment: @post.comments.build
.media.mb-4
.media-body
- @post.comments.each do |comment|
%h5.mt-0= comment.content
%h5.mt-0= comment.user_name
%h2 Edit user
= link_to edit_post_comment_path([@post, comment]) do
%button.btn.btn-primary{:type => "button"} Primary
= link_to "Delete Comment", [@post, comment], method: :delete, data: { confirm: 'Are you sure?' }
comments_controller.rb
before_action :set_post, only: [:create]
def create
@comment = @post.comments.new(comment_params)
@comment.user_name = current_user.nickname
if @comment.save!
redirect_to @post
else
flash.now[:danger] = "error"
end
end
def edit
@post = Post.find(params[:post_id])
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def comment_params
params[:comment].permit(:content, :post_id)
end
def post_params
params.require(:comment).permit(:content)
end
def set_post
@post = Post.find(params[:post_id])
end
routes.rb
resources :users, only: :show
resources :posts do
resources :comments
end
root 'home#index'
root :to => redirect("/users/sign_in")
Upvotes: 1
Views: 946
Reputation: 511
use before_action in
Comments Controller
for comments also
before_action :set_comment, only: [:show, :edit, :update, :destroy]
.....
.....
private
def set_comment
@comment = @post.comments.find(params[:id])
end
and then in views/comments/show.html.haml
edit_post_comment_path(@post, @comment)
give it a try...
Upvotes: 0
Reputation: 17735
Instead of
link_to edit_post_comment_path([@post, comment]) do
try
link_to edit_post_comment_path(@post, comment) do
or alternatively
link_to [:edit, @post, comment] do
See https://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Upvotes: 1
Reputation: 51191
You should pass two arguments into edit_post_comment_path
method instead of passing an array, like this:
edit_post_comment_path(@post, comment)
Upvotes: 1