1

I have two models: List and Item. While adding items, I need to get list_id:

Here is my code:

In items_controler.rb

def new
  @item = Item.new
end

In app/models/item.rb

class Item < ActiveRecord::Base
    belongs_to :list
    attr_accessible :list_id
end

And this is my form:

<%= form_for(@item, remote: true, :html => { :role => "form" }) do |f| %>

<div id="error_explanation" class="bg-danger text-danger"></div>

<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            <%= f.label :name, :class => "control-label" %>
            <%= f.text_field :name, :class => "form-control first_input"  %>
        </div>
        <div class="form-group">
            <%= f.label :color, :class => "control-label" %>
            <%= f.text_field :color, :class => "form-control"  %>
        </div>
        <div class="form-group">
            <%= f.radio_button :priority, 'top', :checked => true %> 
            <%= label :priority_top, 'Place at the top of the list' %><br/>
            <%= f.radio_button :priority, 'bottom' %>
            <%= label :priority_bottom, 'Place at the bottom of the list' %>
            <%= f.hidden_field :list_id, :value => params[:list_id] %>
        </div>
        <div class="form-group">
                <%= f.submit  @item.new_record? ? "Create Item" : "Update Item", :class => "btn btn-primary" %>
        </div>
    </div>
</div>

<% end %>

Now em getting the error on page load

undefined method `attr_accessible' for #<Class:0xb3a2f928>

Is there any different way to do this?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
anujeet
  • 167
  • 2
  • 14

3 Answers3

1

If you define belongs_to :list, you have automatic access to the list and list_id attributes, so you don't have to define any attribute accessors.

And BTW, attr_accessible is deprecated since Rails 2.3.8.

zwippie
  • 15,050
  • 3
  • 39
  • 54
0

You don't need attr_accessible :list_id in your Item class: just delete this line.

The database should have a field items.list_id, and Rails will automatically make getter/setters for this field, and the belongs_to association will add a bunch more methods to do with the item->list relationship.

If you want to control the permitted parameters, do it in the controller.

How is attr_accessible used in Rails 4?

Community
  • 1
  • 1
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • i need to use list_id to pass it to hidden field in view , how should i pass it to view through controller's new action? – anujeet Dec 15 '15 at 10:30
  • How you're using it in the form is fine, i think, it's hard to say as i don't really know what you're trying to do. – Max Williams Dec 15 '15 at 10:31
  • i'm saving item information with ajax but each item is associated to a list by many to one relationship , so i need list_id to save in item table – anujeet Dec 15 '15 at 10:32
  • Yep, and is that not working properly at the moment? – Max Williams Dec 15 '15 at 10:35
0

Looks like you need nested resources:

#config/routes.rb
resources :lists do
   resources :items, only: [:new, :create] #-> url.com/lists/:list_id/items/new
end

#app/controllers/items_controller.rb
class ItemsController < ApplicationController
    def new
       @list = List.find params[:list_id]
       @item = @list.items.new
    end

    def create
       @list = List.find params[:list_id]
       @item = @list.items.new list_params
       @item.save
    end

    private

    def list_params
       params.require(:list).permit(:x, :y, :z)
    end
end 

This will allow you to access the list object, and create a nested items object for it to use.

--

You'll have to change your form_for to use the nested route path:

<%= form_for [@list, @item] do |f| %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • and the other routes for items remains unchanged right? – anujeet Dec 15 '15 at 10:38
  • Sure, unless you want them to be available outside of a `list` -- in which case you'd add `resources :items` on its own – Richard Peck Dec 15 '15 at 10:38
  • Now my link to new item's form is <%= link_to "add new item", new_item_path, remote: true, class: "btn btn-success" %> How it will change? – anujeet Dec 15 '15 at 10:47