How to specify post path for rails partial form - ruby-on-rails

I have a partial form that creates a post that is being rendered on it's parent model communities, however, it doesn't post the form to this path:
POST /communities/:community_id/posts(.:format) posts#create
Instead, it will try posting to the path it's rendered on. For example, No route matches [POST] "/communities/1" because I have the form on a community page.
This is running Rails 6 beta.
I believe a solution would be to specify the path for where it sends to in the form, but I cannot find anything in the documentation that matches that. Either I'm reading wrong, or it's a nonexistent solution and requires a different approach. I'm really not sure.
posts/_form.html.erb
<%= form_with model: #post, local: true do |form| %>
...
<% end %>
routes.rb
resources :communities do
resources :posts
end

You can just use the url option, i.e:
form_for #post, local: true, url: posts_path do |form|
Use the path helper, you can find it's name by doing rake routes

Related

No route matches [POST] "/articles/new" - Why?

I made the following introduction:
http://guides.rubyonrails.org/getting_started.html
And after that, I wanted to create a new field (like title) and got the error notification:
Here is my code:
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
resources :description
root 'welcome#index'
# For details on the DSL available within this file, see guides.rubyonrails.org/routing.html
end
end
I know that this question has been asked a lot but their solutions didn't helped me.
What should I do?
Regards
The problem is in your new.html.erb form, you are missing url: articles_path, if you don't specify it, the form will be sent to the same action (i.e. new). So, since submitting the form uses POST (by default) method, you get:
No route matches [POST] “/articles/new”
You need to change your form_for and specify the url, like this:
<%= form_for :article, url: articles_path do |f| %>
From the same link your provided:
There's one problem with this form though. If you inspect the HTML
that is generated, by viewing the source of the page, you will see
that the action attribute for the form is pointing at /articles/new1.
This is a problem because this route goes to the very page that you're
on right at the moment, and that route should only be used to display
the form for a new article.
1 My emphasis.
By default rails new resource route has a GET HTTP verb, not POST. Unless you specified otherwise.
Seems like you are using new_article_path or articles/new in the form
<%= form_for :article, url: new_articles_path do |f| %>
Change it to
<%= form_for :article, url: articles_path do |f| %>

Rails: Error on form_tag custom action

About a year and a half ago I took an online course in Ruby on Rails using version 3.2. I got through the class and made the application and now I'm going back through the class PDFs and doing it in Rails 4.1.0.
I've got to a section where I'm getting an error and I'm not sure if I'm doing something wrong or if something changed from Rails 3.2 to 4.1.
I've got this form_tag tag:
<%= form_tag :action => 'review', :id => #restaurant do %>
<strong>Poster: </strong><%= text_field "review", "poster" %><br /><br />
<strong>Date: </strong><%= datetime_select "review", "date" %><br /><br />
<strong>Review:</strong><br />
<%= text_area "review", "review", :rows => 5 %><br />
<%= submit_tag "Review" %>
<% end %>
The instructions say to get this to work to put the following code in the restaurants_controller.rb file:
def review
Restaurant.find(params[:id]).reviews.create(params[:review])
redirect_to :action=>"show", :id => params[:id]
end
But when I try and view the page that has the for for it I get this error:
No route matches {:action=>"review", "controller=>"restaurants", :id=>#<Restaurant id: 1, name: "Marco & Luca", created_at: "...", updated_at: "..."
My rake routes shows that show is mapped to the restaurants#show is mapped to the restaurant path.
Is the form_tag piece wrong?
In the instructions from the class we didn't set up anything in routes.rb, I'm guessing that's because of the redirect_to line in the controller file. Is that deprecated now? Is that no longer valid?
Routes file as requested: (in the class exercise we didn't have to edit the routes file).
Rails.application.routes.draw do
# You can have the root of your site routed with "root"
root 'restaurants#index'
resources :restaurants do
collection do
get 'login'
get 'register'
post 'newuser'
post 'validate'
post 'search'
end
end
Thanks!
You do need a route for that. The way it works is:
User fills in a form on some page and sends data to review route
Server processes user's data and returns a redirect to show route
A user gets a redirect and requests the show route
A server executes the show action
Apparently the route for review action (or as I called it above: a review route) doesn't exist in routes.rb. Add it.
UPD: Your routes file looks really odd, you seem to not follow a common practice to process one type of resources in one controller. Here, you probably need to add:
...
post 'search'
post 'review'
# ^ THIS ^
end
end
But you already process at least 3 types of resources in one controller: Users, Sessions and Restaurants. This might not be your fault if you were told to do so, but in either case it's not a proper way to structure an application.
Try:
<%= form_tag :action => 'review', :id => #restaurant.id do %>

Rails form_for wrong method

I've got a weird issue with one of my forms. The form only changes one variable of the object, called admin_comment. It ends up in the show method of the controller, in the terminal:
Processing by EnrolmentsController#show as
Here is the form_for
<%= form_for enrolment, :url => enrolments_admin_comment_path(enrolment), method: :get, remote: true do |f| %>
And here is the rake routes
enrolments_admin_comment GET /enrolments/admin_comment(.:format) enrolments#admin_comment
And the routes.rb part
get "enrolments/admin_comment"
resources :courses do
resources :enrolments
end
When I delete the method: :get part from the form_for, it ends up at the update method.
Everything else with the enrolments controller/model works fine. Does somebody know what's going on here? Thanks!
Update
After nearly getting insane I've got it working like this, but only with :get as method.
routes.rb
get "/enrolments/:id/admin_comment" => "enrolments#admin_comment", as: "enrolments_admin_comment"
form_for
<%= form_for enrolment, :url => enrolments_admin_comment_path(enrolment), method: :get, remote: true do |f| %>
If I change the method to :post, I get the following error:
ActionController::RoutingError (No route matches [POST] "/enrolments/28/admin_comment"):
Two questions came up:
1. What do I have to change to make it work with :post?
2. As far as I understand, if I just would just state the controller and the verb (:get / :post / ...), rails would know which method it has to use as the verbs are mapped to the methods. But when I state the whole path (controller and method), shouldn't rails know everything it needs without the verb? The form params are being sent anyway.
Update2
Ok, I've changed
get "/enrolments/:id/admin_comment" => "enrolments#admin_comment", as: "enrolments_admin_comment"
to
post "/enrolments/:id/admin_comment" => "enrolments#admin_comment", as: "enrolments_admin_comment"
Now everything works fine.
You'll be best looking at Rails' Resource Routing
HTTP Verbs
Each resources :controller you create in your routes.rb file creates a series of routes, which connect with relative HTTP Verbs:
The HTTP verbs part of the routing system is the most important, as it governs which controller action is loaded. You can use the same path helper with different HTTP Verbs to route to completely different controller actions
If you want to create a new path, you'd need to set the HTTP verb to method: :post, like this:
<%= form_for enrolment, :url => enrolments_admin_comment_path(enrolment), method: :post, remote: true do |f| %>
Routes
Perhaps you'd be better with this routing structure:
resources :courses do
resources :enrolments do
get :admin_comment, shallow: :true
end
end

NoMethodError when rendering a form Rails

routes.rb:
resources :shops
shop_controller.rb:
def new
#shop=Shop.new
end
new.html.erb:
<%= form_for(#shop) do |f| %>
....
<% end %>
error:
undefined method `shops_path' for:
<%= form_for(#shop) do |f| %>
The problem is that I already specify the shop resources in the routes file.
Why still get such kind of error?
Any help will be appreciated, thanks
You should use ShopsController not ShopController due to Rails naming convention.
Make sure you have these lines in your rake routes output:
shops GET /shops(.:format {:action=>"index", :controller=>"shops"}
POST /shops(.:format) {:action=>"create", :controller=>"shops"}
OR
shops POST /shops(.:format) {:action=>"create", :controller=>"shops"}
If they aren't present, look carefully at your routes.rb for possible with_options, scope or any other scoping that can affect your resources :shops in such a way that it doesn't generate default url helpers.
since u havent specified the method in the form tag, i guess it is going as a GET request. Try adding the method to ur form
<%= form_for(#shop), :method => :post do |f| %>

Rails: form_for namespaced resource

I want to set up the CRUD for users, available just for administrators of my web application.
So in routes.rb:
namespace :admin do
resources :user
end
which means this:
admin_user_index GET /admin/user(.:format) admin/user#index
POST /admin/user(.:format) admin/user#create
new_admin_user GET /admin/user/new(.:format) admin/user#new
edit_admin_user GET /admin/user/:id/edit(.:format) admin/user#edit
admin_user GET /admin/user/:id(.:format) admin/user#show
PUT /admin/user/:id(.:format) admin/user#update
DELETE /admin/user/:id(.:format) admin/user#destroy
Show, index work fine but edit and new don't. I keep getting this error in the _form first line:
undefined method `user_path' for #<#:0x007fb6645c6378>
which is this:
How can I use form_for with a namespaced resource?
You can add the namespace's name as a symbol:
<%= form_for [:admin, #user] do |f| %>
When you use form_for, it assumes that the appropriate path is [model_name]_path. You have to explicitly tell it the url with
form_for #user, url: admin_user_path

Resources