Why does this form think it should point to an index?
I have a simple_form_for new_comment which results in undefined method
comments_path ' error in the browser when I just try to view the form (don't submit)
_form.html.slim
= simple_form_for new_comment, :remote => true do |f|
This is partial, so the local variable is passed from the hacks scaffold show page
show.html.slim - hacks
= render partial: "comments/form", locals: { new_comment: @new_comment } if user_signed_in?
I am defining @new_comment in hacks controller
hacks_controller.rb
def show
@hack = Hack.find(params[:id])
@comments = @hack.comment_threads.order('created_at DESC')
@new_comment = Comment.build_from(@hack, current_user.id, "") if user_signed_in?
#build_from is a method provided by the acts_as_commentable_with_threading gem
end
Why does new_comment want to redirect to comment_path? I can't even imagine the form.
routes.rb
root 'hacks#index'
concern :commentable do
resources :comments, only: [:create, :update, :destroy]
end
resources :hacks, concerns: [:commentable]
resources :users
devise_for :users, :skip => [:sessions, :registration]
devise_for :user, :path => '', :path_names => { :sign_in => "login",
:sign_out => "logout",
:sign_up => "register",
:account_update => "account-settings" }
source to share
Routes
First, I don't think your form will be routed to an action index
The call comments_path
depends on the CRUD actions that you have defined in your routes. Specifically, it form_for
will auto-populate for the action create
that Rails will try to call from a set of routes based on resource
that you define in your file routes.rb
:
Take a look at the example above, how does it go /photos
with a verb POST
? It may seem like ( photos_path
), but in fact, due to the HTTP verb, it will perform the actioncreate
The form
simple_form_for
is basically an abstraction form_for
:
In the examples provided, although not explicitly stated, we still need to use the: url option to indicate where the form will be submitted. However, further simplification is possible if the entry passed to form_for is a resource, i.e. a set of RESTful routes, for example. defined using the resource method in config /routes.rb. In this case, Rails will simply output the corresponding URLs from the post itself.
Basically, it form_for
tries to create url
to send from the objects that it has. You have a nested route, which means you need to provide nested objects:
= simple_form_for [hack, new_comment], :remote => true do |f|
That should send the path to hacks_comments_path
what you want, right? Alternatively, you can specify an option url
:
= simple_form_for new_comment, remote: true, url: hacks_comments_path(hack) do |f|
Note that both of these fixes require a local variable hack
?
= render partial: "comments/form", locals: { new_comment: @new_comment, hack: @hack } if user_signed_in?
Fix
You need to pass your path nested
to the helper simple_form_for
(as described above)
source to share
Since your comments are nested in hacks, you need both a comment and a hack. So try this
show.html.slim
= render partial: "comments/form", locals: { new_comment: @new_comment, hack: @hack } if user_signed_in?
_form.html.slim
= simple_form_for [hack, new_comment], :remote => true do |f|
source to share