参数缺失或值为空:{:user

编程入门 行业动态 更新时间:2024-10-28 09:29:11
本文介绍了参数缺失或值为空:{:user_id=>9}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看过类似的问题,但我不知道如何解决.我做了几次不成功的尝试.我收到此错误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 end

propuneres/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

本文发布于:2023-10-22 20:56:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缺失   值为   参数   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!