Rails 4 Controller for Reviews Create 方法传递了所有正确的参数,但是它没有正确创建

Rails 4 Controller for Reviews Create method is passed all the correct parameters however it does not create properly

*********解决方案******** <%= f.hidden_field :property_id, :value => @property.id %> 隐藏字段未正确分配给未能传递给控制器​​的 f。谢谢史蒂夫·克莱因


我目前正在开发一个人们可以 post 和查看属性的网站。 我是 运行 Ruby 2.2.0 和 Rails 4.2.3.

我目前有一个 属性 class,它也显示属于 class 的评论。还有一个简单的create view界面通过partial form呈现。 Review Create 方法需要

  1. 描述
  2. user_id
  3. property_id

create 方法通过参数正确接收了 :description 和 :user_id 但是 property_id 没有被正确分配。 review.rb 中的验证接受描述和 user_id 但不接受 property_id。

  1. Property.find_by_id(参数[:id]).id 在_review_form.html.erb
  2. @property.id 在show.html.erb

两者都正确 return 视图中当前 属性 的 ID。

谁能看出我做错了什么?任何帮助将不胜感激。

class Review < ActiveRecord::Base
              belongs_to :property
              belongs_to :user
              default_scope -> { order(created_at: :desc) }
              validates :user_id,       presence: true
              validates :property_id,   presence: true
            end

class ReviewsController < ApplicationController
      before_action :logged_in_user, only: [:create, :destroy]

      def create
        #Errors out @review = Review.build(review_params) Review.build method does not exist
        #@review = current_user.reviews.build(review_params)
        @review = Review.new(review_params)
        @review.user_id = current_user.id
        @review.save
        if @review.save
          flash[:success] = "Review created!"
        else
          flash[:failure] = "Try Again"
        end
        redirect_to :back

      end

      def destroy
      end

      private

        def review_params
          params.require(:review).permit(:description, :property_id)
        end
    end


class CreateReviews < ActiveRecord::Migration
  def change
    create_table :reviews do |t|

      t.text :description
      t.references :property, index: true, foreign_key: true
      t.references :user, index: true, foreign_key: true
      t.timestamps null: false
    end
    add_index :reviews, [:user_id, :created_at]
    add_index :reviews, [:property_id, :created_at]
  end
end

    <!--Applicable Code in properties/show.html.erb-->
    <% if logged_in? %>
      <div class="row" style="width:25% centered">
          <section class="review_form">
            <!-- @property.id correctly returns the id of the current property -->
            <%= render 'shared/review_form', locals: {property_id: @property.id} %>
          </section>
      </div>
    <% end %>

<!-- properties/show.html.erb -->
    <% provide(:title, @property.title) %>
      <!-- PROP TITLE -->
      <div class="centered">
        <h2><%=@property.title.upcase%></h2>
        <h4><%=@property.address_street %></h4>
        <h4>Owned by <%=@property.user.name%></h4>
      </div>
    <% form_for @property,  :url=> {:action=> (@property.new_record? ? 'index' : 'new')} do |f| %>

    <% end %>
    <!-- PUTTING IN THE SLIDER -->
      <div id="owl-demo" class="owl-carousel owl-theme">          
        <div class="item"><%= image_tag "lroom1.jpg" %></div>
        <div class="item"><%= image_tag "lroom2.jpg" %></div>
        <div class="item"><%= image_tag "room1.jpg" %></div>
        <div class="item"><%= image_tag "room2.jpg" %></div>
        <div class="item"><%= image_tag "room3.jpg" %></div>
      </div>
    <!-- DESCRIPTION TABLE -->
    <div style="background-color:#F8F8F8; padding-top:30px ">
    <table class="descrip centered">
      <tr >
        <td>Description</td>
        <td class="col-content"><%=@property.summary %></td>
      </tr>
      <tr>
        <td>The place</td>
        <td class="col-content">Has <%=@property.num_bedroom %> beds</td>
      </tr>
      <tr>
        <td></td>
        <td class="col-content">Has <%=@property.num_bath %> bathroom</td>
      </tr>
      <tr>
        <td>Owner</td>
        <td class="col-content"><%=@property.title %></td>
      </tr>
      <tr>
        <td>Address</td>
        <td class="col-content"><%=@property.address_street %></td>
      </tr>
    </table>
    <br />

    <div>
    <h1> Hello World <%= Property.find_by_id(params[:id]).id%> </h1>

    <!--<body style="margin:0px; padding:0px;" onload="initialize()">-->
    <table class="centered reviews">
      

      <% @reviews.each do |d| %>
        <tr>  
        <td>
          <%= image_tag d.user.image %>
          <p ><%= d.user.name %></p>
        </td>
        <td class="col-content">
            <p>Contact at <%= d.user.email %><br /><br />"<%= d.description %>"</p>
        </td>
        </tr>
      <% end %>
      
    </table>

    <% if logged_in? %>
      <div class="row" style="width:25% centered">
          <section class="review_form">
            <!-- @property.id correctly returns the id of the current property -->
            <%= render 'shared/review_form', locals: {property_id: @property.id} %>
          </section>
      </div>
    <% end %>






    <%= javascript_tag do %>
      window.address = '<%= j @property.address_street %>';
    <% end %>

     <input id="pac-input" class="controls" type="text"
            placeholder= "<%=@property.address_street %>" >
     <div id="map_canvas" style="width:100%; height:500px;"></div>

    </div>



    <script>
    $(document).ready(function() {
     
      $("#owl-demo").owlCarousel({
     
          autoPlay: 3000, //Set AutoPlay to 3 seconds
     
          items : 4,
          itemsDesktop : [1199,3],
          itemsDesktopSmall : [979,3]
     
      });
     
    });
    </script>

  <!-- partial form _review_form.html.erb -->
  
  <%= form_for(@review) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="centered">
   <h1> Property ID is <%=Property.find_by_id(params[:id]).id%>
   </h1>
  </div>
  <div class="field">
    <%= f.text_area :description, placeholder: "Create A Review" %>
  </div>
  <!-- Property.find_by_id(params[:id]).id correctly returns the id of the current property -->
<!-- The Following Line Returns The Error Message Below-->
  <%= hidden_field_tag(:property_id, property_id) %> 
  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

错误消息:#<#:0x007fbc03fcf820>

的未定义局部变量或方法“property_id”

当您呈现评论表单时,您在当地人中传递了 size: @property.id,但您没有使用 size。视图中的 @property 实例变量在部分视图中不可用。另外,不确定为什么要调用此变量 "size".

因此,在您的主视图中,将 <%= render 'shared/review_form', locals: {size: @property.id} %> 替换为 <%= render 'shared/review_form', locals: {property_id: @property.id} %>。在您的部分中,将 <%= hidden_field_tag(:property_id, Property.find_by_id(params[:id]).id) %> 替换为 <%= hidden_field_tag(:property_id, property_id) %>