思路:后台添加约饭地点后,怎么调取数据。

今天要新加一个model来储存约饭的地点和详细信息,不让用户自己填写约饭地点,必须选择后台给出的约饭地点。 自己首先建立了restaurant的model,又建立了post_restaurant的model,让post和restaurant通过post_restaurant 来建立关系。 最初我只在admin后台写出了restaurant的name,用select box赋给eat_venue一个restaurant的name字段,可以显示成功。 但是想实现的功能和预期的不同,我们需要一个restaurant的name和一个详细地址(description),所以在restaurant里面加入了一个字段。发现需要引用restaurant的所有数据,需要跟post_restaurant里面的两个参数来实现 (post_id,restaurant_id),所以在创建post的时候需要把这两个参数传进去。

  def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    if @post.save
       @post_restaurant = PostRestaurant.new
       @post_restaurant.post_id = @post.id
       @post_restaurant.restaurant_id = @post.eat_venue
       @post_restaurant.save
      redirect_to posts_path
    else
      render :new
    end
  end

给eat_venue传一个restaurant的id,通过这个id来建立关系。这个post就可以显示restaurant的数据了。 但是在edit的时候,修改eat_venue的数值的时候,rails s的log里面已经显示数据传到了后端,但是在前端的显示不变, 用rails c查看参数是否上传,用p = Post.last去找最后一个post,p.restaurant.name去找约饭的地点,发现这个数据并没有被更新,再输入p.post_restaurant发现restaurant_id也没有被更新,再输入p.post_restaurant.restaurant显示数据还是最初创建的。总结原因应该是在更新post的时候,post_restaurant的数据并没有被更新,去修改post_controller里面的update这个action。

  def update
    @post = Post.find(params[:id])
    @post_restaurant = @post.post_restaurant
    if @post.update(post_params)
       @post_restaurant.restaurant_id = @post.eat_venue
       @post_restaurant.save
    
      redirect_to posts_path, notice: 'Update Success'
    else
      render :edit
    end
  end

下面是rails console的记录

Development [2] eatgether(main)> p = Post.last
  Post Load (0.8ms)  SELECT  "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT ?  [["LIMIT", 1]]
#<Post:0x007fd75ad2f4b8> {
             :id => 10,
          :title => "第二周遇到最大的坑",
    :description => "fefef",
     :created_at => Sun, 27 Nov 2016 11:30:30 UTC +00:00,
     :updated_at => Sun, 27 Nov 2016 11:30:39 UTC +00:00,
        :user_id => 1,
      :eat_venue => "2",
        :eat_day => Sat, 26 Nov 2016,
          :image => #<ImageUploader:0x007fd75ad24158 @model=#<Post id: 10, title: "第二周遇到最大的坑", description: "fefef", created_at: "2016-11-27 11:30:30", updated_at: "2016-11-27 11:30:39", user_id: 1, eat_venue: "2", eat_day: "2016-11-26", image: nil>, @mounted_as=:image>
}
Development [3] eatgether(main)> p
#<Post:0x007fd75ad2f4b8> {
             :id => 10,
          :title => "第二周遇到最大的坑",
    :description => "fefef",
     :created_at => Sun, 27 Nov 2016 11:30:30 UTC +00:00,
     :updated_at => Sun, 27 Nov 2016 11:30:39 UTC +00:00,
        :user_id => 1,
      :eat_venue => "2",
        :eat_day => Sat, 26 Nov 2016,
          :image => #<ImageUploader:0x007fd75ad24158 @model=#<Post id: 10, title: "第二周遇到最大的坑", description: "fefef", created_at: "2016-11-27 11:30:30", updated_at: "2016-11-27 11:30:39", user_id: 1, eat_venue: "2", eat_day: "2016-11-26", image: nil>, @mounted_as=:image>
}
Development [4] eatgether(main)> p.restaurant.name
  Restaurant Load (0.2ms)  SELECT  "restaurants".* FROM "restaurants" INNER JOIN "post_restaurants" ON "restaurants"."id" = "post_restaurants"."restaurant_id" WHERE "post_restaurants"."post_id" = ? LIMIT ?  [["post_id", 10], ["LIMIT", 1]]
"陈老板的家里hi"
Development [5] eatgether(main)> p.post_restaurant
  PostRestaurant Load (0.2ms)  SELECT  "post_restaurants".* FROM "post_restaurants" WHERE "post_restaurants"."post_id" = ? LIMIT ?  [["post_id", 10], ["LIMIT", 1]]
#<PostRestaurant:0x007fd758d522a8> {
               :id => 10,
          :post_id => 10,
    :restaurant_id => 1,
       :created_at => Sun, 27 Nov 2016 11:30:30 UTC +00:00,
       :updated_at => Sun, 27 Nov 2016 11:30:30 UTC +00:00
}
Development [6] eatgether(main)> p.post_restaurant.restaurant
  Restaurant Load (0.2ms)  SELECT  "restaurants".* FROM "restaurants" WHERE "restaurants"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
#<Restaurant:0x007fd758ad37c0> {
             :id => 1,
           :name => "陈老板的家里hi",
     :created_at => Sun, 27 Nov 2016 09:47:49 UTC +00:00,
     :updated_at => Sun, 27 Nov 2016 10:06:31 UTC +00:00,
    :description => "重庆新马泰高级豪华别墅110024栋"
}