【问题标题】:Rails 4 Controller for Reviews Create method is passed all the correct parameters however it does not create properlyRails 4 Controller for Review Create 方法传递了所有正确的参数,但它没有正确创建
【发布时间】:2015-10-13 02:17:10
【问题描述】:

*********解决方案******** @property.id %> 隐藏字段未正确分配给 f 未能传递给控制器​​。谢谢史蒂夫克莱恩


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

我目前有一个属性类,它也显示属于该类的评论。还有一个通过部分表单呈现的简单创建视图界面。 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(params[:id]).id 在 _review_form.html.erb
  2. @property.id 在 show.html.erb

两者都正确返回视图中当前属性的 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 %>

错误消息:#

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

【问题讨论】:

    标签: html ruby-on-rails ruby model-view-controller ruby-on-rails-4.2


    【解决方案1】:

    当您呈现评论表单时,您在本地传递了size: @property.id,但您没有使用size。视图中的 @property 实例变量在部分中将不可用。另外,不知道你为什么称这个变量为“size”。

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

    【讨论】:

    • 您好@steve klein,感谢您的帮助。 size: @property.id 是以前尝试正确使 create 方法正常工作的错误。 1. Property.find_by_id(params[:id]).id 在 _review_form.html.erb 2. @property.id 在 show.html.erb 中都正确返回视图中当前属性的 id。所以在 _review_form.html.erb 中的第一行代码应该可以工作。当我尝试您的修复时,我收到错误“未定义的局部变量或方法‘property_id’”您是否看到任何其他错误?你知道我该如何解决上述错误吗?
    • 在您的帖子中,您更新了主表单,所以我看到您将 property_id 传递给您的部分表单,因此它应该在该范围内。你能在你的帖子中更新你的部分以显示它在那里被使用并指出哪一行代码出错了吗?另外,请确认您没有从其他地方调用相同的部分。
    • 我已经更新了上面的代码以及它显示的错误消息 -> # 的未定义局部变量或方法 `property_id'。我意识到的一个重要细节是,即使设置了正确的 property_id,控制器仍然无法创建评论,因为 property_id 将保持为 nil 并且不会通过存在真实验证。谢谢您希望很快收到您的来信
    • @property.id 是 nil 还是填充在主视图中 (show)?从您的 cmets 来看,它似乎已正确填充,甚至直到调用渲染部分。如果是这样,部分中的property_id 是否未设置为与@property.id 相同的值?
    • @property.id in show.html.erb 已正确填充。在部分形式property_id 中是未定义的局部变量或方法“property_id”。在部分中,:property_id 和 @property.id 都是 nil。
    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多