0

Hey guys not sure what's going on here. I have Movies and Critics on my app. I've set up an assocation between those and Reviews. I'm trying to set up the controller and form to create and destroy Reviews. In the rails console I can create reviews that belong to both just fine and I have tested my controller (may be incorrect though) and it seems to be working, so I think the problem is in my form. Thanks in advance guys. Here's the codeand server logs:

class ReviewsController < ApplicationController

def create
    @movie = Movie.find(params[:movie_id])
    current_critic.reviews.create(content: params[:content], movie_id: @movie.id)
    redirect_to @movie
end

def destroy
    @movie = Movie.find(params[:movie_id])
    @review = current_critic.reviews.find_by(movie_id: @movie.id)
    @review.delete
    redirect_to @movie
end

end

form:

<div class="form">
<h1 class="smaller">Write a Review</h1>
<%= form_for(current_critic.reviews.new) do |r| %>
<%= hidden_field_tag :movie_id, @movie.id %>
<ul>
    <li>
        <%= r.text_area :content, placeholder: "Write your review...", size: "50x10" %>
    </li>

    <li>
        <%= r.submit "Submit Review" %>
    </li>
</ul>
<% end %>
</div>

server log after submitting form:

Started POST "/reviews" for 99.39.164.184 at 2015-12-04 20:34:59 +0000
Processing by ReviewsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"d16BVZxzqZY5bQrw9xr2VlWbWjh0Dc7bL6t4OgKQPk1RXWt40acMjtkjXG9DUBBfnA7K06iJDwQzd5YJ0D6c4Q==", "movie_id"=>"2", "review"=>{"content"=>"One last try at writing and submitting a review before I head out"}, "commit"=>"Submit Review"}
  Movie Load (2.1ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1  [["id", 2]]
  Critic Load (0.2ms)  SELECT  "critics".* FROM "critics" WHERE "critics"."id" = ? LIMIT 1  [["id", 1]]
   (5.3ms)  begin transaction
   (0.9ms)  commit transaction
Redirected to https://everyones-a-critic-caedbudris.c9users.io/movies/2
Completed 302 Found in 574ms (ActiveRecord: 18.1ms)

EDIT: I implemented strong paramaters for create so the controller is now

def create
    @movie = Movie.find(params[:movie_id])
    current_critic.reviews.create(review_params)
    redirect_to @movie
end
private

    def review_params
        params.require(:review).permit(:content, :movie_id)
    end

And it is now inserting into reviews, but for some reason it's not getting the movie_id passed by the hidden_field_tag. Why is this?

Started POST "/reviews" for 99.39.164.184 at 2015-12-05 21:31:07 +0000
Processing by ReviewsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OlBIfneoWTvBtIeISTF9ubo9jj06oVyfDd6rswxe7xO+JyGXRvFV4TLD+3xKhBZHRF+eRJAawKUabU7KrLpZow==", "movie_id"=>"2", "review"=>{"content"=>"review review review review review"}, "commit"=>"Submit Review"}
  Movie Load (0.3ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1  [["id", 2]]
  Critic Load (0.3ms)  SELECT  "critics".* FROM "critics" WHERE "critics"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (6.4ms)  INSERT INTO "reviews" ("content", "critic_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["content", "review review review review review"], ["critic_id", 1], ["created_at", "2015-12-05 21:31:08.185722"], ["updated_at", "2015-12-05 21:31:08.185722"]]
   (10.3ms)  commit transaction
Redirected to https://everyones-a-critic-caedbudris.c9users.io/movies/2
Completed 302 Found in 157ms (ActiveRecord: 25.6ms)
3
  • Where is current_critic defined in ReviewController#create? If it is defined in a helper method, where is that? Commented Dec 5, 2015 at 0:18
  • Yeah it's defined in the sessions helper for logging in. I just finished adding a favorite function and it worked fine for that. Hmmm that may be the problem cause it doesn't look like it's passing the correct critic id. Commented Dec 5, 2015 at 0:22
  • Nevermind it is passing the correct id Commented Dec 5, 2015 at 0:39

1 Answer 1

1

You should whitelist your parameters, by default rails won't accept any parameters to avoid mass assignment. Proper way is to define a protected block at the bottom of your controller. Like this,

protected
def rating_params 
params.require(:rating).permit(:content)
end

And you can use it like

current_critic.reviews.create(rating_params)
Sign up to request clarification or add additional context in comments.

3 Comments

How could I implement @movie.id into that? Would this work? def review_params params.require(:review).permit(:content, :movie_id) end
I still have the same problem :/
can you post your models and current_critic helper to look pls.

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.