零值而不是形式值

时间:2016-04-01 21:44:00

标签: ruby-on-rails ruby activerecord

我的页面索引为Beer Receipe,也应该显示评分/评论。我在rails c中插入了一对,并在Beer Receipe页面上显示正常。但是当我尝试在表单上创建评级时,表单上的星号和注释都会显示为空白,当我查看rails c时,它会显示nil值。为什么?

有三种型号:Beer_Receipe,Ingredient,&评分

这是架构:

  create_table "beer_receipes", force: :cascade do |t|
    t.string   "name"
    t.string   "style"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "ingredients", force: :cascade do |t|
    t.string   "name"
    t.string   "category"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "ratings", force: :cascade do |t|
    t.string   "ratable_type"
    t.integer  "ratable_id"
    t.integer  "stars"
    t.text     "comments"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

他们的联系是:

class BeerReceipe < ActiveRecord::Base
    has_many :ratings, as: :ratable, dependent: :destroy
end

class Ingredient < ActiveRecord::Base
    has_many :ratings, as: :ratable, dependent: :destroy
end

class Rating < ActiveRecord::Base
    belongs_to :ratable, polymorphic: true
end

这是Beer Receipe Index

<p id="notice"><%= notice %></p>

<h2>Beer Receipe Index</h2>

<p>
  <strong>Name:</strong>
  <%= @beer_receipe.name %>
</p>

<p>
  <strong>Style:</strong>
  <%= @beer_receipe.style %>
</p>

<h2>Ratings</h2>
<%= render @beer_receipe.ratings %>

<h2>Add Ratings</h2>
<%= render 'ratings/form' %>

<%= link_to 'Edit', edit_beer_receipe_path(@beer_receipe) %> |
<%= link_to 'Back', beer_receipes_path %>

这是_form.html.erb页面

<%= form_for([@beer_receipe, @beer_receipe.ratings.build]) do |f| %>
    <p>
        <%= f.label :stars %><br>
        <%= f.select :stars, (0..10) %>
    </p>
    <p>
        <%= f.label :comments %><br>
        <%= f.text_field :comments %>
    </p>
    <p>
        <%= f.submit %>
    </p>
    <% end %>

最后,但并非最不重要的,评级控制器

class RatingsController < ApplicationController
  def new
  end

  def create
    @beer_receipe = BeerReceipe.find(params[:beer_receipe_id])
    @rating = @beer_receipe.ratings.create(rating_params)
    #redirect_to beer_receipe_path(@beer_receipe)
  end

  def destroy 
    @beer_receipe = BeerReceipe.find(params[:beer_receipe_id])
    @rating = @beer_receipe.ratings.find(params[:id])
    @rating.destroy
    redirect_to beer_receipe_path(@rating)
  end

  def edit
  end

  def update
  end

  private 

    def rating_params
        params.require(:rating).permit(:star, :comment)
    end
end

修改

Started POST "/beer_receipes/1/ratings" for 127.0.0.1 at 2016-04-01 15:14:43 -0700
Processing by RatingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3kilQgA3tZ0ZxsfuRY4LKFkNl3+oL1x9zRT2/EccDeM0b+6a1NuHgo6cHaYOqBJMd6s4dduID06i5oTvcBisTw==", "rating"=>{"stars"=>"6", "comments"=>"testing .."}, "commit"=>"Create Rating", "beer_receipe_id"=>"1"}
  [1m[35mBeerReceipe Load (0.2ms)[0m  SELECT  "beer_receipes".* FROM "beer_receipes" WHERE "beer_receipes"."id" = ? LIMIT 1  [["id", 1]]
Unpermitted parameters: stars, comments
  [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
  [1m[35mSQL (0.6ms)[0m  INSERT INTO "ratings" ("ratable_id", "ratable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["ratable_id", 1], ["ratable_type", "BeerReceipe"], ["created_at", "2016-04-01 22:14:43.982989"], ["updated_at", "2016-04-01 22:14:43.982989"]]
  [1m[36m (38.6ms)[0m  [1mcommit transaction[0m
  Rendered ratings/create.html.erb within layouts/application (0.1ms)
Completed 200 OK in 437ms (Views: 351.6ms | ActiveRecord: 40.8ms)


Started GET "/" for 127.0.0.1 at 2016-04-01 15:16:03 -0700
Processing by BeerReceipesController#index as HTML
  [1m[35mBeerReceipe Load (0.8ms)[0m  SELECT "beer_receipes".* FROM "beer_receipes"
  Rendered beer_receipes/index.html.erb within layouts/application (14.7ms)
Completed 200 OK in 686ms (Views: 668.5ms | ActiveRecord: 1.3ms)


Started GET "/beer_receipes/2" for 127.0.0.1 at 2016-04-01 15:16:06 -0700
Processing by BeerReceipesController#show as HTML
  Parameters: {"id"=>"2"}
  [1m[36mBeerReceipe Load (0.6ms)[0m  [1mSELECT  "beer_receipes".* FROM "beer_receipes" WHERE "beer_receipes"."id" = ? LIMIT 1[0m  [["id", 2]]
  [1m[35mRating Load (0.3ms)[0m  SELECT "ratings".* FROM "ratings" WHERE "ratings"."ratable_id" = ? AND "ratings"."ratable_type" = ?  [["ratable_id", 2], ["ratable_type", "BeerReceipe"]]
  Rendered ratings/_rating.html.erb (1.3ms)
  Rendered ratings/_form.html.erb (8.1ms)
  Rendered beer_receipes/show.html.erb within layouts/application (90.6ms)
Completed 200 OK in 140ms (Views: 134.6ms | ActiveRecord: 1.1ms)


Started POST "/beer_receipes/2/ratings" for 127.0.0.1 at 2016-04-01 15:16:11 -0700
Processing by RatingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WmwvDG+a/YsgGKpOGtlxQLUCNjyc/+y3lhab5ZO9WPCwS2TUu3bPlLdCcAZR/2gkm6SZNu9Yv4T55On2pLn5XA==", "rating"=>{"stars"=>"3", "comments"=>"sdfsdf"}, "commit"=>"Create Rating", "beer_receipe_id"=>"2"}
  Rendered ratings/create.html.erb within layouts/application (0.3ms)
Completed 200 OK in 104ms (Views: 101.5ms | ActiveRecord: 0.0ms)

1 个答案:

答案 0 :(得分:0)

在您的评级控制器创建操作中尝试此操作:

@rating = Rating.new(stars: params[:rating][:star], comments: params[:rating][:comments], ratable_id: params[:beer_recipe_id])

if @rating.save
  puts "huzzah!"
end

如果有效,请告诉我。