【发布时间】:2015-10-13 02:17:10
【问题描述】:
*********解决方案******** @property.id %> 隐藏字段未正确分配给 f 未能传递给控制器。谢谢史蒂夫克莱恩
我目前正在开发一个人们可以发布和查看属性的网站。 我正在运行 Ruby 2.2.0 和 Rails 4.2.3。
我目前有一个属性类,它也显示属于该类的评论。还有一个通过部分表单呈现的简单创建视图界面。 Review Create方法需要
- 说明
- user_id
- property_id
create 方法通过参数正确接收 :description 和 :user_id 但未正确分配 property_id。 review.rb 中的验证接受描述和 user_id,但不接受 property_id。
- Property.find_by_id(params[:id]).id 在 _review_form.html.erb
- @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