0

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

ルート.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 に答える