Rails:表单保存不正确 [英] Rails: Form isn't saving properly

查看:41
本文介绍了Rails:表单保存不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a basic application for practice, you input a movie and when it was rented on. The move form works fine, the title of the movie links to the rental date. But the rentals form doesn't seem to work. I don't get an error or anything, it just doesn't save. I'm assuming the issue is with my create method but I can't figure it out.

movies_controller.rb

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

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(租赁)

new.html.erb (rentals)

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(电影)

new.html.erb (movies)

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

推荐答案

问题的根源(如您在评论中提到的)来自@movie = Movie.find(params[:movie_id]),所以我想说您的,看起来应该像这样:

The root of the problem (as you mentioned in the comments) is coming from the @movie = Movie.find(params[:movie_id]) so I would say that you have the problem in your routes.rb, which should look something like this:

resources :movies do
  resources :rentals
end

现在,用于创建新的rental的链接为/movies/:movie_id/rentals/new,路径为new_movie_rental.您可以编写类似这样的内容以链接到新的租赁页面:

now the link for creating a new rental would be /movies/:movie_id/rentals/new and the path would be new_movie_rental. You can write something like this to link to the new rentals page:

<%= link_to new_movie_rental_path(@movie) %>

这篇关于Rails:表单保存不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆