[英]Rails conditional statement
我不知道標題標題是好的還是床位,因為我是紅寶石的新手,我在困擾一個有條件的問題,例如下面的示例。
我有三個表,如user
, post
和saved_post
用戶表
user_id | user_name |
---------------------
1 | ABC |
---------------------
2 | efg |
發布表
post_id | title |
--------------------
1 | XYZ |
--------------------
2 | xyz |
saved_post表
id | user_id | post_id |
-----------------------------
1 | 1 | 2 |
視圖
<% @post.each do |p| %>
<%= p.post_title %>
<%= form_for :create, url: home_path(@save), action: :create, method: :post do |f| %>
<%= f.hidden_field :post_id, :value => p.post_id %>
<button class="btn btn-default btn-save" type="submit">Save</button>
<% end %>
<% end %>
如果user_id 1
將post_id 2
保存在saved_post
表中,則顯示此帖子僅針對user_id 1
保存,否則顯示save。
我該如何解決?
您可以嘗試has_many :through
Active Record具有的關聯。 請參考此鏈接 。
class Users < ActiveRecord::Base
has_many :posts
has_many :saved_posts, through: :posts
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :saved_post
end
class SavedPost < ActiveRecord::Base
has_many :posts
has_many :users, through: :posts
end
您可以在上面的鏈接中找到關於此的更多詳細信息。
Rails完全是關於約定而不是配置。 這意味着,如果按照Rails約定編寫Rails應用程序,您將從中受益匪淺。
您的表應以復數形式命名。 表格和帖子。 我從您的評論中了解到,用戶有很多帖子,並且帖子可以屬於許多用戶。 這是典型的has_and_belongs_to_many關聯。
如果您將save_post表重命名為posts_users(兩個表名均按字母順序排列),Rails將知道如何自動處理這些表。
數據庫應如下所示:
我制作了一個示例應用程序,其中包含您的兩個用戶和方法,以向其中兩個用戶添加帖子。
# routes.rb
root 'posts#index'
resources :posts
# posts_controller.rb
class PostsController < ApplicationController
before_action :set_user
def index
@posts = @current_user.posts
end
def new
@post = @current_user.posts.build(title: 'totally new title')
end
def edit
@post = Post.find(params[:id])
end
def create
@post = Post.new(post_params)
@post.save
redirect_to posts_path
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to posts_path
else
render 'edit'
end
end
# --------------------------------------------------------------------------
private
# --------------------------------------------------------------------------
# Sets the user based on the params value
def set_user
if params[:user_id]
@current_user = User.find(params[:user_id])
else
# If not found, sets the first from the Users table
@current_user = User.first
end
end
def post_params
params.require(:post).permit(:title, {user_ids: []})
end
end
指數
# index.html.erb
<h3>Users</h3>
<% User.all.each do |user| %>
<%= link_to user.username, posts_path(user_id: user.id) %> <br />
<% end %>
<h3>Posts for the user: <%= @current_user.username %></h3>
<p>
<% @posts.each do |post| %>
<%= link_to post.title, edit_post_path(post) %> <br />
<% end %>
</p>
<p>
<%= link_to 'Create new post', new_post_path %>
</p>
在編輯中,您可以選擇與該帖子相關的用戶:
# edit.html.erb
<%= render :partial => "post_form", locals: {button_name: 'update'} %>
# _post_form.html.erb
<h3>Post <%= @post.title %> for the user <%= @current_user.username %></h3>
<%= form_for @post do |f| %>
<%= f.text_field :title %> <br />
<p>
<label>
<%= f.collection_check_boxes(:user_ids, User.all, :id, :username) %>
<label><br />
</p>
<p>
<%= f.submit button_name, class: "btn btn-default btn-save" %>
</p>
<% end %>
在post.rb和user.rb文件中,您需要指定這兩個類之間的關聯。 並且當您正確命名posts_users表時,Rails會自動找到它。
# post.rb
class Post < ActiveRecord::Base
has_and_belongs_to_many :users
end
# user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :posts
end
如果您創建一個新標題,則將使用相同的形式:
# new.html.erb
<%= render :partial => "post_form", locals: {button_name: 'create'} %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.