Keep params after render Ruby on Rails

前端 未结 2 479
终归单人心
终归单人心 2021-01-18 01:04

I have a Project that belongs to User. In my user view I have a link to add a new project, with the parameter for the user I want to add the project to.

<         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 01:21

    You shouldn't use params[:id] to assign value to this form field. Instead, add this to your #new action in controller:

    def new
      @project = Project.new(user_id: params[:id])
    end
    

    and then just write this in your form:

    <%= f.hidden_field :user_id %>
    

    Because @project was defined in your #new and #create actions and because it already contains a Project instance with a user_id assigned to it, the value would automatically be added to this field.

提交回复
热议问题