James White
James White

Reputation: 535

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!

Upvotes: 0

Views: 288

Answers (2)

D-side
D-side

Reputation: 9485

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.

Upvotes: 1

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

Try:

<%= form_tag :action => 'review', :id => @restaurant.id do %>

Upvotes: 0

Related Questions