I have a project owned by the user. In my user view, I have a link to add a new project, with a parameter for the user to whom I want to add the project.
<%= link_to 'Add new project', :controller => "project", :action => "new", :id => @user %> Url: /projects/new?id=62
Adding a project to the user works. The problem is when validation fails when adding a new project and I am doing the rendering.
def create @project = Project.new(params[:project]) if @project.save redirect_to :action => "show", :id => @project.id else render :action => "new" end end
View:
<%= form_for @project do |f| %> <%= f.label :name %> <%= f.text_field :name %> <%= f.hidden_field :user_id , :value => params[:id] %> <%= f.submit "Create project" %> <% end %>
routes
resources :users do resources :projects end
How to save a parameter for a user after rendering? Or is there a better way to do this? Looking at a lot of similar questions, but can't get it to work.
source share