Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails can';t在数据库中插入以下\u id_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails can';t在数据库中插入以下\u id

Ruby on rails can';t在数据库中插入以下\u id,ruby-on-rails,ruby,Ruby On Rails,Ruby,这是我针对以下用户的简单web应用,它有一个错误,请帮助我:) 我无法将以下\u id插入数据库。我受够了 *这是我的应用程序控制器 class ApplicationController < ActionController::Base protect_from_forgery with: :exception include WelcomeHelper end class RelationController < ApplicationController def

这是我针对以下用户的简单web应用,它有一个错误,请帮助我:) 我无法将以下\u id插入数据库。我受够了

*这是我的应用程序控制器

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include WelcomeHelper
end
class RelationController < ApplicationController
  def create
    follow = User.find(params[:relation][:following_id])
    current_user.following << follow
    redirect_to current_user
  end
class WelcomeController < ApplicationController
  def index
  end
 def create
    user = User.find_by_username(params[:session][:username])
      if user
      login user
      redirect_to user
    else
      render 'index'
    end
    end
 def sucess
   @users = User.all
   @relation = Relation.new
 end
end
*关系控制器

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include WelcomeHelper
end
class RelationController < ApplicationController
  def create
    follow = User.find(params[:relation][:following_id])
    current_user.following << follow
    redirect_to current_user
  end
class WelcomeController < ApplicationController
  def index
  end
 def create
    user = User.find_by_username(params[:session][:username])
      if user
      login user
      redirect_to user
    else
      render 'index'
    end
    end
 def sucess
   @users = User.all
   @relation = Relation.new
 end
end
*成功观

Following
<ul>
  <% current_user.following.each do |u| %>
    <li><%= link_to u.username, u %></li>
  <% end %>
</ul>
Followed By
<ul>
  <% current_user.followers.each do |u| %>
    <li><%= link_to u.username, u %></li>
  <% end %>
</ul>
List Users<br />
<% if !@users.blank? %>
<% for @user in @users %>
<%= @user.username%><br />
    <%= form_for @relation do |f| %>
      <%= f.hidden_field :following_id, :value => @user.id %>
      <%= f.submit "Follow" %>
    <% end %>
<%end%>
<%else%>
<%end%>

当我在rails控制台中检查Relation.all时,下面的\u id已经被插入,但是当我检查(作为当前用户帐户)user.following时-我什么也看不到,没有后面的\u id。我认为Relation\u controller在“current\u user.following中出现问题,您需要先清理参数,然后才能将内容放入数据库


阅读更多内容

我已经为我的当前用户创建了会话。当我单击“跟随”时,发送的是不同的id,而不是我的当前用户id。我认为这不是一个无关紧要的问题“Rails 4强参数”我不知道如何修复:(
  get "welcome/sucess"
  get "welcome/error"
  root :to => "welcome#index"
  get '/users/:id', :to => 'welcome#sucess', :as => "user"
  match '/relations', to: 'relation#create', via: 'post'

  resources :users
  resources :posts
  resources :relations

  post   'login'   =>  'welcome#create'
Following
<ul>
  <% current_user.following.each do |u| %>
    <li><%= link_to u.username, u %></li>
  <% end %>
</ul>
Followed By
<ul>
  <% current_user.followers.each do |u| %>
    <li><%= link_to u.username, u %></li>
  <% end %>
</ul>
List Users<br />
<% if !@users.blank? %>
<% for @user in @users %>
<%= @user.username%><br />
    <%= form_for @relation do |f| %>
      <%= f.hidden_field :following_id, :value => @user.id %>
      <%= f.submit "Follow" %>
    <% end %>
<%end%>
<%else%>
<%end%>
{"utf8"=>"✓",
 "authenticity_token"=>"NxOq/F5tOuElvhJNLOvkt/25enUN1wDI05I0fKp998Q=",
 "relation"=>{"following_id"=>"11"},
 "commit"=>"Follow"}