erb - Rails Comments ID => nil -


i have comments table , users comment on movie. comment belongs_to :movie , movie has_many :comments

my problem when create new comment, field has there <%= f.text_field :movie_id %>, , if remove it,

no route matches {:action=>"show", :controller=>"movies", :id=>nil}

this absolute path create new comment http://localhost:3000/movies/:movie_id/comments/new

i've checked action in movies controller , can't see anything. i've played create action in comments controller still nothing.

having field in view can quite confusing user. have solution this?

this *comments_controller.rb*

class commentscontroller < applicationcontroller   # /comments   # /comments.json   before_filter :load_movie   before_filter :authenticate_user!, only: [:create,:destroy,:edit,:destroy]    ...       # /comments/new   # /comments/new.json   def new     @movie = movie.find(params[:movie_id])     @comment = @movie.comments.new      @search = movie.search(params[:q])      respond_to |format|       format.html # new.html.erb       format.json { render json: @comment }     end   end     # post /comments   # post /comments.json   def create      @comment = comment.new(params[:comment])      @comment.user_id = current_user.id      @search = movie.search(params[:q])      respond_to |format|      if @comment.save        @movie = @comment.movie        format.html { redirect_to movie_path(@movie), notice: "comment created" }        format.json { render json: @comment, status: :created, location: @comment }      else        format.html { render action: "new" }        format.json { render json: @comment.errors, status: :unprocessable_entity }      end    end  end  private   def load_movie     @movie = movie.find_by_id(params[:movie_id])   end end 

routes.rb

appname::application.routes.draw    resources :comments    resources :movies       resources :comments, only: [:create,:destroy,:edit,:destroy,:new]       member          'comments'       end   end end 

*_form.html.erb*

<%= form_for(@comment) |f| %>   <%= f.error_notification %>    <div class="form-inputs">     <%= f.text_field :subject %>     <%= f.text_area :body %>     <%= f.text_field :movie_id %>   </div>    <div class="form-actions"> <%= f.submit "sumbit review" %>   </div> <% end %> 

resources :movies   resources :comments, only: [:create,:destroy,:edit,:destroy,:new] end 

will give url like:

../movies/:movie_id/comments/new 

remove text_field movie_id not safe.

in controller create action:

@movie = movie.find(params[:movie_id]) @comment = @movie.comments.new(params[:comment])  @comment.user_id = current_user.id 

not sure why need:

member    'comments' end 

form

when using nested resource you'll need build form this:

<%= form_for ([@movie, @comment]) |f| %>   <%= f.some_input %> <% end %> 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -