创建一个“POST”表单

时间:2013-03-03 22:54:37

标签: ruby-on-rails-3 ruby-on-rails-3.2

我不确定如何创建POST表单。我目前有一个表单,用户输入并在我的电影表中创建一个条目。我希望有一个按钮,它接受一组给定的属性,并在用户只点击一个按钮后添加到表中。

在我的搜索控制器中,我有:

def index
  @movie = Movie.new   
end

这是搜索的索引视图:

<%= 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 %> 

目前它路由到[http:// localhost:3000 / search.4]理想情况下我希望它路由到[http:// localhost:3000 / movies / 4]

路线:

   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

1 个答案:

答案 0 :(得分: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| %>