简体   繁体   中英

Ruby on rails. Passing params from view to controller

I'm having what I assume must be a simple problem but I just can't figure it out. I'm trying to update an attribute in one model when another is created.

In my view:

<%= link_to 'Click here to rate this user', new_user_review_path(:user_id => request.user.id, :gigid => request.gig.id), remote: true %>

Which passes params :gigid and :user_id

Than my controller:

def new
    @review = Review.new
    @gig = Gig.find(params[:gigid])
end

  def create
    @review = @user.reviews.new review_params
    @review.reviewed_id = current_user.id
       if @review.save
        @gig.update(reviewed: true)
        respond_to do |format|
          format.html {redirect_to session.delete(:return_to), flash[:notice] = "Thankyou for your rating!"}
          format.js 
        end
    else
      render 'new'
    end
  end

But I get undefined method 'update'for nil:NilCLass:

I know the params are passing and the 'Gig' can be updated as :

def new
    @review = Review.new
    Gig.find(params[:gigid]).update(reviewed: true)
end

updates the attribute fine, but when I click 'New review' not when the review is actually created.

Adding :

def create
    @review = @user.reviews.new review_params
    @review.reviewed_id = current_user.id
      if @review.save
        Gig.find(params[:gigid]).update(reviewed: true) 

etc etc etc 

gives me the same undefined method 'update'for nil:NilCLass:

I have tried with find_by_id instead of find which makes no difference.

EDIT:

def create
@gig = Gig.find params[:gigid] 
@review = @user.reviews.new review_params
    @review.reviewed_id = current_user.id
       if @review.save
         @gig.update(reviewed: true)
    etc etc etc

Doesn't work either. I get no errors, but the gig ID is still 'nil'.

The params are passing to the 'New' action but not the 'Create' action. I feel this should be very easy but I'm just not seeing it at the moment.

The problem is, you never assigned a value to @gig in your create method. I can't see your form, but you need something like this in your create method:

@gig = Gig.find params[:gigid]

Assuming that you're passing the parameter :gigid to #create

In the second example you showed, I'm not sure what's going on, but you should be getting a ActiveRecord::RecordNotFound exception on the find().

But I get undefined method 'update'for nil:NilCLass:

The error is that you have not defined @gig in your create action.

Since Rails is built on HTTP, and HTTP is stateless , you have to set the "instance" variables with each new request:

def new
  @review = Review.new
  @gig    = Gig.find params[:gigid]
end

def create
 @gig    = Gig.find params[:gigid]
 @review = @user.reviews.new review_params

A much better pattern for you would be to use the after_create callback in your Review model:

#app/models/review.rb
class Review < ActiveRecord::Base
   belongs_to :gig #-> I presume
   after_create :set_gig

   private

   def set_gig
      self.gig.update(reviewed: true)
   end
end

--

If you wanted to make the Gig update within your current setup, you'll be best sending the gig_id param through the request (not the link):

#app/views/reviews/new.html.erb
<%= form_for [@user, @review] do |f| %>
   <%= f.hidden_field :gig_id, @gig.id %> #-> params[:reviews][:gig_id]
   ...
<% end %>

This will make params[:review][:gig_id] available in the create action, with which you'll be able to use in your code.

Try the below code for update operation.

gig_record = Gig.find_by_id(params[:gigid])
gig_record.update_attribute(reviewed: true) unless gig_record.blank?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM