【问题标题】:Post and Location Association with ancestry gem与祖先宝石的邮政和位置关联
【发布时间】:2014-07-17 10:33:16
【问题描述】:

我的 Rails 应用程序有两个模型。位置和帖子,位置有很多帖子。我正在使用 祖传宝石。

class Post < ActiveRecord::Base
 belongs_to :location, :counter_cache => true
end

class Location < ActiveRecord::Base
 include Tree
 has_ancestry :cache_depth => true
 has_many :posts
end

我的帖子控制器

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

def index
@posts = Post.all
end

def show
end

def new
 @post = Post.new
end


 def edit
 end

def create
 @post = Post.new(post_params)

 respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render action: 'show', status: :created, location: @post }
  else
    format.html { render action: 'new' }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end
end

def update
respond_to do |format|
  if @post.update(post_params)
    format.html { redirect_to @post, notice: 'Post was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end
end

def destroy
@post.destroy
respond_to do |format|
  format.html { redirect_to posts_url }
  format.json { head :no_content }
end
end

private
 # Use callbacks to share common setup or constraints between actions.
 def set_post
   @post = Post.find(params[:id])
 end

 # Never trust parameters from the scary internet, only allow the white list through.
 def post_params
  params.require(:post).permit(:name)
 end
end

如果我在 _form.html.erb 中创建 Location 所属的新帖子

<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
 <div id="error_explanation">
  <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:   </h2>

  <ul>
  <% @post.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
 <%= f.label :name %><br />
 <%= f.text_field :name %>
</div>

<%= select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %> 

<div class="actions">
 <%= f.submit %>
</div>

浏览器显示错误信息如下所示

ArgumentError in Posts#new

【问题讨论】:

  • 请发布确切的错误信息。
  • Posts#new 中出现此错误
  • 后控制器有什么问题吗?我想在 def new 中添加任何代码

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 ancestry


【解决方案1】:

不确定这是否能解决您的错误,但是:

要使下拉菜单正常工作,请将select 行更改为:

<%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>

这是因为您希望表单构建器f 处理表单元素的创建。

您还必须将控制器中的:location_id 参数列入白名单:

def post_params
  params.require(:post).permit(:name, :location_id)
end

【讨论】:

  • 我的帖子 indexhtm.erb 如何显示帖子位置? > 不工作只有工作是 如何显示帖子位置名称?
  • post show.html.erb &lt;%= @post.location.name%&gt;error NoMethodError in Posts#show
  • 你能帮我吗? @zwippie
  • &lt;%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %&gt;如何显示location.name
  • 谁能帮忙解答这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多