Association between Post and Location

隐身守侯 提交于 2019-12-14 03:34:04

问题


This my first ruby on rails application.

Model Location and Post, Location has many post.I create location as tree structure with ancestry gem.

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

This my Post Controller

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
 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, :location_id)
 end
end

Creating Post with location in Post _form.html.erb

<%= simple_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 %>

<%= f.input :name %>

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

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

My question are

First question- f.select :location_id , how display location name, not location id, i am using with simple form

Second question- Post index got error in <%= post.location.name %>

<% @posts.each do |post| %>
 <tr>
  <td><%= post.name %></td>
  <td><%= post.location.name  %></td>
  <td><%= link_to 'Show', post %></td>
  <td><%= link_to 'Edit', edit_post_path(post) %></td>
  <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>  </td>
</tr>
<% end %>

回答1:


First question:

Check the simple_form syntax for a dropdown. It is mentioned in the docs and you should be able to get this working by yourself.

Second question:

Does the offending post really have a related location? If it does not have one, it can not display the name of course. To counter these nil errors, use try:

<%= post.location.try(:name)  %>

try will call the method name on location only if location is not nil.




回答2:


For your first question :

Maybe you should read about "options_for_select"

http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects

<% cities_array = City.all.map { |city| [city.name, city.id] } %>
<%= options_for_select(cities_array) %>

For your second question: What is your error ?

Maybe one of your "post.location" is nil. If so, try:

post.location.name unless post.location.nil?

Hope this can help



来源:https://stackoverflow.com/questions/24822886/association-between-post-and-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!