0

Railsは初めてです。コレクションを作成して友達と共有するためのシンプルなアプリに取り組んでいます。アイテムを持っているユーザーがいます。これらのアイテムはカテゴリに属しており、サブカテゴリがあります。ユーザーはアイテムにコメントを付けることができます

ユーザーhas_manyアイテム、アイテムbelongs_toユーザー

カテゴリhas_manyアイテム、アイテムはカテゴリに属します

カテゴリhas_manyサブカテゴリ、サブカテゴリbelong_toカテゴリ

アイテムhas_manyコメント、コメントはアイテムに属します。

コメントを作成するためのとは別に、カテゴリとサブカテゴリを選択するオプションを作成することができました。これにより、itemsテーブルのcategory_idとsubcategory_idが自動的に更新されます。

views / item / new.html.erb

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

 <div class="field">
    <%= f.label :category %><br />
    <%= f.collection_select :category_id, @categories, :id, :category_type, prompt: true %>
 </div>

  <div class="field">
    <%= f.label :subcategory %><br />
    <%= f.collection_select :subcategory_id, @subcategories, :id, :sub_type, prompt: true %>
 </div>

 <div class="field">
  <%= f.label :description %><br />
  <%= f.text_area :description, rows: 7%>
 </div>

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

controllers / items_contoller.rb

クラスItemsController<ApplicationController

   def index
    @items = Item.all

  end

  def new
    @item = Item.new
    @categories = Category.all
    @subcategories = Subcategory.all 

  end

  def edit
    @item = Item.find(params[:id])
    @categories = Category.find.all
    @subcategories = Subcategory.all 

  end

  def show
    @item = Item.find(params[:id])

     render 'show'
    end

  def create
    @item = Item.new(params[:item])

    if @item.save
      flash[:success] = "Added new item #{@item}"
      redirect_to @item
    else
      render 'new'
    end
  end

  def update
    @item = Item.find(params[:id])

    if @item.update_attributes(params[:item])
     flash[:success] = "Successfully updated item!"
    redirect_to @item 
    else
     render 'edit'
    end  
  end

  def destroy
    @item = Item.find(params[:id])
    @item.destroy
    flash[:success] = "Successfully deleted category!"
    redirect_to categories_url, notice: "deleted"
  end

end

しかし、作成または編集するときに特定のアイテムをユーザーに関連付けるのに問題があります。私はすでにユーザーのための作業セッション管理を持っています。ユーザーはログインおよびログアウトできます。また、itemsテーブルのuser_id列を、現在のセッションのユーザーの対応するuser_idで更新できるようにしたいのですが、どうすればよいかわかりません。それが役に立ったら、以下は私がセッション管理を処理する方法です

session_controller.rb

クラスSessionsController<ApplicationController

 def new
  end

  def create
    user = User.find_by_email(params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
     render 'new', notice:"Invalid email/password combination" 
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

session_helper.rb

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def current_user?(user)
    user == current_user
  end 

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

予想される応答に事前に感謝します

4

1 に答える 1

1
class ItemsController < ApplicationController
  def create
    @item = Item.new(params[:item])
    @item.user_id = current_user.id

    # ...
  end
end

それがあなたが探しているものであるかどうかわからない。

于 2012-12-14T14:28:47.123 に答える