【问题标题】:Rails: Form isn't saving properlyRails:表单未正确保存
【发布时间】:2015-01-29 07:59:00
【问题描述】:

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

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

耙路线:

 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

【问题讨论】:

  • roguerat - 您可以检查@rental 的值是多少,然后打印该值。并尝试指定方法= POST 或 PUT

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


【解决方案1】:

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

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

【讨论】:

  • 嗯,我收到“找不到带有 'id'= 的电影”错误。它在此处突出显示这一行“@movie = Movie.find(params[:id])”
  • 等等,应该是params[:movie_id] 而不是params[:id],看我的编辑
  • 我更改了它,但仍然遇到同样的错误。 “在“@movie = Movie.find(params[:movie_id])”上找不到带有 'id'=" 的电影
  • 你能告诉我你是如何打开表单的代码吗?我的意思是链接和与之关联的控制器代码..
  • 刚刚用movies_controller.rb 和new.html.erb 更新了原版的电影。这些是我现在所有的控制器和视图文件。
【解决方案2】:

问题的根源(正如您在 cmets 中提到的)来自@movie = Movie.find(params[:movie_id]),所以我想说您的routes.rb 中存在问题,应该如下所示:

resources :movies do
  resources :rentals
end

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

<%= link_to new_movie_rental_path(@movie) %>

【讨论】:

  • 当我更改我的路线并链接到此并转到 /movies/:movie_id/rental/new 时,我收到 No route matches [GET] "/movies/:movie_id/rental/new" 错误。我还尝试转到旧路径 /movies/new,我认为它现在不起作用,但我收到一个错误提示 No route matches {:action=&gt;"new", :controller=&gt;"rentals", :movie_id=&gt;nil} missing required keys: [:movie_id],以防万一
  • @roguerat 将:movie_id 替换为现有Movie 的ID。还要确保在更改 routes.rb 后重新启动服务器。
  • 嗯,重新启动我的服务器并尝试localhost:3000/movies/5/rental/new 和其他一些数字。仍然得到同样的错误。我现在也无权访问任何东西。我曾经能够访问我的电影列表......标题是我点击的链接,它会将我带到租赁表格,我唯一的问题是它没有保存。但现在没有任何效果。
  • 这对我来说似乎很好。您可以通过执行bundle exec rake routes 查看可用路由列表。你遇到了什么错误?
  • 嗯,我刚试了,没有报错。如果有帮助,我在上面添加了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2020-09-01
相关资源
最近更新 更多