0

I am not sure how to create a POST form. I currently have a form that takes users input and create an entry in my movies table. I want to have a button that takes a set of given attributes and is added to the table once a user only clicks a button.

In my searches controller I have:

def index
  @movie = Movie.new   
end

This is in the index view of searches:

<%= simple_form_for(@movie, :url => { :action => "create" }) do |f| %>
    <%= f.input :title, :as => :hidden, :input_html => { :value => "Skyfall" } %>
    <%= f.input :year, :as => :hidden, :input_html => { :value => "2012" } %>
    <%= f.input :description, :as => :hidden, :input_html => { :value => "James Bond" } %>
    <%= f.association :genres, include_blank: false, :as => :hidden, :input_html => { :value => "some value" } %>
    <%= f.button :submit, class: "btn btn-warning" %>
<% end %> 

Currently it routes to [http://localhost:3000/search.4] Ideally I want it to route to [http://localhost:3000/movies/4]

routes:

   searches GET    /searches(.:format)          searches#index
            POST   /searches(.:format)          searches#create
 new_search GET    /searches/new(.:format)      searches#new
edit_search GET    /searches/:id/edit(.:format) searches#edit
     search GET    /searches/:id(.:format)      searches#show
            PUT    /searches/:id(.:format)      searches#update
            DELETE /searches/:id(.:format)      searches#destroy
     movies GET    /movies(.:format)            movies#index
            POST   /movies(.:format)            movies#create
  new_movie GET    /movies/new(.:format)        movies#new
 edit_movie GET    /movies/:id/edit(.:format)   movies#edit
      movie GET    /movies/:id(.:format)        movies#show
            PUT    /movies/:id(.:format)        movies#update
            DELETE /movies/:id(.:format)        movies#destroy
     search GET    /search(.:format)            movies#search
     genres GET    /genres(.:format)            genres#index
            POST   /genres(.:format)            genres#create
  new_genre GET    /genres/new(.:format)        genres#new
 edit_genre GET    /genres/:id/edit(.:format)   genres#edit
      genre GET    /genres/:id(.:format)        genres#show
            PUT    /genres/:id(.:format)        genres#update
            DELETE /genres/:id(.:format)        genres#destroy
       root        /                            movies#index

routes.rb

Movies::Application.routes.draw do
  resources :searches
  resources :movies
  get 'search', to: 'movies#search', as: :search

  resources :genres
  root :to => 'movies#index'
end
4

1 回答 1

1

那是因为你是从搜索控制器/视图调用它,尝试类似:

<%= simple_form_for @movie do |f| %>

或者

<%= simple_form_for @movie, url: movie_path(@movie) do |f| %>

或最坏的情况(因为它将是固定路径):

<%= simple_form_for @movie, url: movies_path, method: post do |f| %>
于 2013-03-04T03:49:54.953 回答