7

How can I change params inside controller?

When I click the accept it will pass the status is approved but if diff <= 0 change status to rejected

View

<%= link_to 'Accept', friend_path(s, :request => {:status => 'Accepted'}), method: :put  %>

into this

    if diff <= 0
     req_params[:status] = "Rejected" 
     @request.update(req_params)
    end
end 

private 
 def req_params
    params.require(:request).permit(:status)
 end

end
8
  • Is req_params is params filtered by strong params? (require,permit) Commented Nov 9, 2017 at 12:55
  • Can you paste the whole controller method? rails doesn't have req_params method Commented Nov 9, 2017 at 13:02
  • You want to use button_to or a regular form and not link_to and pass the parameters in the request body instead of the URL. Using link_to with method: option should only be really be done when the request has no params like link_to "Delete", thing, method: :delete Commented Nov 9, 2017 at 13:09
  • Still... It's not clear what is diff variable? Can you please post all your controller action code? (I think it's update method) Commented Nov 9, 2017 at 13:11
  • Where do you take start and end variables? (I'am asking all of this, because I want to suggest you some really good solution in case of design) Commented Nov 9, 2017 at 13:21

2 Answers 2

11

To modify your status param, you need to do the following:

params[:request][:status] = 'Rejected' 

or

req_params[:request][:status] = 'Rejected'

Because your request take params like this:

Parameters => { some_param => 'Something', required => { permited1 => 'bla', 
                                                     permited2 => 'bla2',
                                                    ... } }

I hope that helps you

Sign up to request clarification or add additional context in comments.

Comments

0

I know this is an old thread, but it took me a few hours to figure this out (I'm a ROR novice) and I figured I'd put my solution in for anyone that came here with my problem.

Don't try to overwrite the fields directly, instead just chain merge:

@request.update(req_params).merge!(:status, "Rejected")

Here is my working snippet. My form gives me a date and time in two different fields, so I need to merge them into a datetime to store in my model. I do that on the fly when calling update and save.

animal_vital.update!(animal_vital_params.merge!( dt: params[:animal_vital][:dt] + " " + params[:animal_vital][:tm]).except(:tm))

You need merge!, not merge. Hope this helps!

Comments

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.