在Rails中为post创建设置user_id

时间:2013-10-15 14:47:42

标签: ruby-on-rails ruby ruby-on-rails-4

我在创建帖子

时设置user_id时遇到问题
def create

  @post = Post.new(post_params)
  @post.user_id = session[:id]

  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

重定向到show动作时,我得到的错误是undefined method 'username' for nil:NilClass。查看数据库显示没有设置user_id。

我也尝试将user_id作为隐藏字段传递。但这也不起作用(看一下日志显示隐藏的字段因为某种原因甚至没有通过)。

欣赏正确方向的一点,

谢谢!

编辑:这里要求的是节目控制器

def show
    @post = Post.find(params[:id])

    @original_id = params[:original_id]

    @comment = Comment.new
end

<%= form_for [:admin, @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 %>

以下是表格

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :category %><br>
    <%= f.text_field :category %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

路线:

root'posts #index'

  resources :posts do
    resources :comments, shallow: true
  end

  resources :users, only: [:create]

  #Posts
  get "/meta", to: "posts#meta"

  #User Routes
  get "/signup", to: "users#new"
  get "/success", to: "users#index"

  #login Routes
  get "/login", to: "sessions#login"
  post "/sessions/login_attempt", to: "sessions#login_attempt"

  #session routes
  get "/sessions/home", to: "sessions#home"
  get "/sessions/logout", to: "sessions#logout"

  #admin routes
  #resources :posts, module: 'admin', except: [:show, :index]
  namespace :admin do 
    root 'posts#new'
    resources :posts, except: [:show, :index]
  end

3 个答案:

答案 0 :(得分:3)

在您的create方法内添加

@post = Post.build(params[:post])
@post.user_id = current_user.id

这将获取给定帖子的当前用户ID。

答案 1 :(得分:0)

如果帖子和用户之间存在has_many关系,为什么不这样做:

@user = User.find(params[:id])
@post = @user.posts.new(params[:post])

params可能会有所不同,但这是“更多红宝石”做事的方式。它明确指出了两个模型之间的关系。上面的代码应该在#new动作中,然后你将它保存在create动作中。

答案 2 :(得分:0)

我现在感觉自己像个白痴,但问题是我使用的是会话[:id],而不是会话[:user_id]

在我的会话控制器中设置

def login_attempt
authorized_user = RegisteredUser.authenticate(params[:username_or_email],params[:login_password])
    if authorized_user
      session[:user_id] = authorized_user.id
      flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
      redirect_to(:action => 'index', :controller => "users")
    else
      flash[:notice] = "Invalid Username or Password"
      flash[:color]= "invalid"
      render "login"    
    end   
end