ruby on rails - Associating nested attributes to user -


i'm trying build small expense tracking app using rails 4.1. using devise authorization. expense , it's nested attribute, comments belong user. associations set in model , expenses getting associated user. here's expense controller:

class expensescontroller < applicationcontroller     def new         @expense = expense.new         @item = @expense.items.build         #@comment = @expense.comments.build     end      def index         @expenses = expense.all         #@items = item.where(:expense_id => @expense.id)     end      def show         @expense = expense.find(params[:id])         @items = item.where(:expense_id => @expense.id)     end      def create         @expense = current_user.expenses.new(expense_params)         respond_to |format|             if @expense.save                 expensemailer.expense_submission(@expense).deliver                 format.html { redirect_to @expense, notice: 'expense report submitted.' }                 format.json { render :show, status: :created, location: @expense }             else                 format.html { render :new }                 format.json { render json: @expense.errors, status: :unprocessable_entity }             end         end     end      def edit         @expense = expense.find(params[:id])     end      def update         @expense = expense.find(params[:id])         #@comment = @expense.comments.build         if @expense.update(expense_params)             #if @comment.save                 #expensemailer.comments_added(@expense).deliver                 flash[:notice] = "expense report updated"                 redirect_to expenses_path             #else             #   flash[:notice] = "expense report updated"                 #redirect_to expenses_path             ##end         else             render 'edit'         end     end 

the form comment attributes built looks like:

    <%= nested_form_for (@expense) |f| %>       <div class="form-group">         <%= f.label :state %><br />         <%= f.select :state, expense.states, :include_blank => false, class: "form-control" %>        </div>        <%= f.fields_for :comments, @expense.comments.build |comment| %>         <div class="form-group">           <%= comment.label :comment%>           <%= comment.text_area :comment, class: "form-control" %>         </div>         <%= comment.hidden_field :commenter %>       <% end %>         <%= f.submit "submit", class: "btn btn-primary" %>     <% end %>   </div> </div> 

the @comment.commenter = current_user isn't adding current user id database. should include in expense controller somewhere?

you have add:

@comment.commenter = current_user 

below if statement. this:

def create   @article = expense.find(params[:expense_id])    if @comment = @expense.comments.create(comment_params)     @comment.commenter = current_user     @comment.save      expensemailer.comments_added(@expense).deliver     redirect_to expenses_path   end end 

and save comment again. in current code you're overwriting @comment object newly created object doing:

@comment = @expense.comments.create(comment_params) 

but haven't set commenter on new object anywhere yet.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -