2

I want to pass an argument from one controller to another in rails. This is first controller:

def create
@grouptorepo = Grouptorepo.new(params[:grouptorepo])
if @grouptorepo.save
  fileUpdate
  redirect_to create_usermailinglist_path, :group_id => @grouptorepo.group_id, :notice => "Group to repo relation created!"
else 
     render "new"
   end
end

And this is the second one:

def create
@g = params[:group_id]
@users = Usertogroup.where("group_id = ?", @g).first

@usermailinglist.user_id = @users.user_id

  if @usermailinglist.save
    redirect_to repositories_path, :notice => "Relation Created!"
  else
    render "new"
  end
end

@g is always nil I have no idea why.

2
  • redirect_to should redirect to a get action. But :create is a post action. what is the use case here? Commented Jun 3, 2013 at 19:23
  • after assigning group to repository admin will have an option to see if users in that group want to get mail notifications about the repository. Commented Jun 3, 2013 at 19:38

1 Answer 1

1

You have a syntax problem with the create_usermailinglist_path helper:

redirect_to create_usermailinglist_path(:group_id => @grouptorepo.group_id)

Also, if you want the flash[:notice]

if @grouptorepo.save
  fileUpdate
  redirect_to(create_usermailinglist_path(:group_id => @grouptorepo.group_id), :notice => "Group to repo relation created!")
else 
  render "new"
end
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for help but it still does not work. undefined method user_id' for nil:NilClass app/controllers/usermailinglists_controller.rb:10:in create' Line 10 is this: @usermailinglist.user_id = @users.user_id
@users seems to refer to several users, not only one. You probably meant @user ?
it doesn't matter because it is only name. After changing it to @user it still doesn't work
ok i made some errors in syntax. Your answer helped me a lot thanks!
@MrYoshiji I find you here again! Greetings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.