简体   繁体   中英

Rails conditional statement

I don't know my heading title good or bed because I'm new in ruby on rails, I'm troubling one conditional issue like below example.

I have three table like user , post & saved_post

user table

user_id | user_name | 
---------------------
   1    |    ABC    |
---------------------
   2    |   efg     |

post table

 post_id  |  title  |
 --------------------
    1     |   XYZ   |
 --------------------
    2     |    xyz  |

saved_post table

 id  |  user_id  |  post_id  |
 -----------------------------
   1 |      1    |    2      |

View

<% @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 %>

If user_id 1 save post_id 2 in the saved_post table then show this post saved only for user_id 1 , otherwise show save.

How can I reach this solution?

You can try has_many :through Association which Active Record have. Refer this link .

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

More details regarding this you will find in above link.

Rails is all about convention over configuration. Meaning if you write your Rails applications following Rails conventions, you'll have huge benefit from it.

Your tables should be named in plural form. Tables and Posts. What I understood from your comments is that user has many posts and posts can belong to many users. This is typical has_and_belongs_to_many association.

If you rename your saved_post table as posts_users (both table names in alphabetical order) Rails will know how to deal with these tables automatically.

Database should look like this:

在此处输入图片说明

I made a sample application with your two user and methods to add posts to either of those users.

  # 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

在此处输入图片说明

# 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>

In edit you can choose which users are attached to this post:

在此处输入图片说明

# 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 %>

In post.rb and user.rb files you need to specify association between these two classes. And when you have posts_users table named correctly, Rails finds it automatically.

# 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

And if you create a new title, if will use the same form:

在此处输入图片说明

# new.html.erb
<%= render :partial => "post_form", locals: {button_name: 'create'} %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM