Rails:表单没有正确保存

时间:2015-01-29 07:59:01

标签: ruby-on-rails forms ruby-on-rails-4

我正在建立一个基本的练习应用程序,你输入一部电影以及它何时被租用。移动表单工作正常,电影的标题链接到租赁日期。但租赁表格似乎不起作用。我没有收到错误或任何错误,它只是没有保存。我假设问题出在我的创建方法上,但我无法弄明白。

movies_controller.rb

class MoviesController < ApplicationController
  def new
    @movie = Movie.new
    @movies = Movie.all
  end

  def create
    @movie = Movie.new(comment_params)
    if @movie.save
      redirect_to new_movie_path
    end
  end

  def comment_params
    params.require(:movie).permit(:title, :year)
  end

end

rentals_controller.rb

class RentalsController < ApplicationController
  def new
    @movie = Movie.find(params[:id])
    @rental = @movie.rentals.build
  end

  def create
    @movie = Movie.find(params[:id])
    @rental = @movie.rentals.build(rental_params[:rental_id])
    if @rental.save
      redirect_to new_rental_path(:id => @movie.id)
    end
  end


  def rental_params
    params.require(:rental).permit(:borrowed_on, :returned_on, :movie_id)
  end

end

new.html.erb(出租)

Movie: <%= @movie.title %> <%= link_to "back", new_movie_path %>
<hr>

<%= form_for @rental, :url => { :action => :create, :id => @movie.id } do |f| %>
  Borrowed on: <%= f.text_field :borrowed_on %> <br>
  Returned on: <%= f.text_field :returned_on %> <br>
  <%= f.submit %>
<% end %>
<hr>

Rentals: 
<% if !@movie.rentals.blank? %>
  <% for item in @movie.rentals %>
    <%= item.borrowed_on %>, <%= item.returned_on %> <br>
  <% end %>
<% else %>
  No rentals yet
<% end %>

new.html.erb(电影)

Enter new movie information <br>


<%= form_for @movie do |f| %>
  Title: <%= f.text_field :title %> <br>
  Year: <%= f.text_field :year %> <br>
  <%= f.submit %>
<% end %>

<hr>

List of all movies: <br>
<% if !@movies.blank? %>
  <table border=1>
    <tr>
      <th> Title </th>
      <th> Year </th>
    </tr>
  <% for item in @movies %>
    <tr>
      <td> <%= link_to item.title, :controller => :rentals, :action => :new, :id => item.id %> </td> 
      <td> <%= item.year %> </td>
    </tr>
  <% end %>
  </table

<% end %>

路线:

Rails.application.routes.draw do

  resources :movies do
    resources :rentals
  end

  root 'movies#new'
end

rake routes:

 Prefix Verb   URI Pattern                                  Controller#Action
        movie_rentals GET    /movies/:movie_id/rentals(.:format)          rentals#index
                      POST   /movies/:movie_id/rentals(.:format)          rentals#create
     new_movie_rental GET    /movies/:movie_id/rentals/new(.:format)      rentals#new
    edit_movie_rental GET    /movies/:movie_id/rentals/:id/edit(.:format) rentals#edit
         movie_rental GET    /movies/:movie_id/rentals/:id(.:format)      rentals#show
                      PATCH  /movies/:movie_id/rentals/:id(.:format)      rentals#update
                      PUT    /movies/:movie_id/rentals/:id(.:format)      rentals#update
                      DELETE /movies/:movie_id/rentals/:id(.:format)      rentals#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
                      PATCH  /movies/:id(.:format)                        movies#update
                      PUT    /movies/:id(.:format)                        movies#update
                      DELETE /movies/:id(.:format)                        movies#destroy
                 root GET    /                                            movies#new

2 个答案:

答案 0 :(得分:0)

你的控制器应该是这样的,

class RentalsController < ApplicationController
  def new
    @movie = Movie.find(params[:movie_id])
    @rental = @movie.rentals.build
  end

  def create
    @movie = Movie.find(params[:movie_id])
    @rental = @movie.rentals.build(rental_params)
    if @rental.save
      redirect_to new_rental_path(@movie)
    end
  end


  def rental_params
    params.require(:rental).permit(:id, :borrowed_on, :returned_on, :movie_id)
  end

end

您的表单应如下所示

<%= form_for [@movie, @rental] do |f| %>
  Borrowed on: <%= f.text_field :borrowed_on %> <br>
  Returned on: <%= f.text_field :returned_on %> <br>
  <%= f.submit %>
<% end %>

答案 1 :(得分:0)

问题的根源(正如您在评论中提到的)来自@movie = Movie.find(params[:movie_id]),所以我会说您routes.rb中存在问题,这应该是这样的:< / p>

resources :movies do
  resources :rentals
end

现在,创建新rental的链接为/movies/:movie_id/rentals/new,路径为new_movie_rental。你可以写这样的东西来链接到新的租借页面:

<%= link_to new_movie_rental_path(@movie) %>