我看过类似的问题,但我不知道如何解决.我做了几次不成功的尝试.我收到此错误ActionController::ParameterMissing in PropuneresController#new参数缺失或值为空:{:user_id=>9}" 当访问 localhost:3000/users/9/propuneres/new .
I have looked at questions like this one but I could not figure out how to solve it. I made several unsuccessful attempts. I get this error "ActionController::ParameterMissing in PropuneresController#new param is missing or the value is empty: {:user_id=>9}" when visiting localhost:3000/users/9/propuneres/new .
routes.rb
Rails.application.routes.draw do get 'users/show' devise_for :users get 'static/home' get 'static/despre' resources :users, only: [:show] do resources :propuneres end root to: 'static#home'propunere.rb
propunere.rb
class Propunere < ActiveRecord::Base belongs_to :user end用户.rb
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :propuneres endpropuneres/new.html.erb
propuneres/new.html.erb
<h2> Propunere Nouă </h2> <%= form_for @propunere, url: user_propuneres_path(@propunere), html: { method: :get } do |f| %> <ul> <% @propunere.errors.full_messages.each do |error| %> <li><%= error %></li> <% end %> </ul> <p> <%= f.label :titlu %><br /> <%= f.text_field :titlu %> </p> <p> <%= f.label :body %><br /> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %>propuneres_controller.rb
propuneres_controller.rb
before_action :prop def new @propunere = Propunere.new() end def create @propunere = Propunere.new @propunere.titlu = params[:propunere][:titlu] @propunere.body = params[:propunere][:body] @propunere.user_id = params[:propunere][:user_id] @propunere.save redirect_to propunere_path(@propunere) end def index end private def prop params.require(user_id: current_user.id).permit(:titlu, :body,) end end我认为至少有两件事我做错了.感谢您的耐心等待.
I think there are at least two things that I am doing wrong. Thank you for your patience.
推荐答案在表单中,必须将用户分配给 url(如果要用作嵌套资源)
At the form, you must assign the user to url (if you want to use as nested resource)
user_propuneres_path(@user, @propunere)在控制器(您将此问题标记为 Rails 3,我假设您没有使用 gem 'strong_parameters'),然后您可以删除方法 prop 并使用此代码进行新建和创建:
At the controller (you tag this question as Rails 3, I assume you are not using the gem 'strong_parameters'), then you can remove the method prop and use this code for new and create:
def new @user = User.find(params[:user_id]) @propunere = @user.propuneres.build end def create @user = User.find(params[:user_id]) @propunere = @user.propuneres.new(params[:propunere]) if @propunere.save redirect_to user_propunere_path(@user, @propunere) else render 'new' end end更多推荐
参数缺失或值为空:{:user
发布评论