开发者

Ruby on Rails Relational Drop Downs Second dependant on the First

I have three different models that will be involved with this. I am trying to create a user who will belong to an owner. The owner has many partners and the partner has many clients. The problem with this and how it varies from other questions asked here, is that I do not want someone to view source of the page and see the entire list of clients. This is currently in devel/testing phase right now. I will eventually restrict the owner_id to be hard coded to the @current_user's owner_id. This owner user can only see their own partners and those partner's clients. I would not want them to see the partners of another owner.

<%= f.input :owner_id, :as => :select, :collection => Owner.find(:all) %>
<%= f.input :partner_id, :as => :select, :collection => Partner.find(params[:owner_id]) %>
<%= f.input :client_id, :as => :select, :collection => Client.find(params[:partner_id]) %>

The issue that I have above is that the find params is generating an error since the :owner_id has no value and is not going to find any partners. So how do I go about having the client selection populate the list of clients when a partner is selected. Likewise, how to figure out how to populate the Partners once the owner is selected. Keep in mind, that the owner line will change once I figure this piece out. There's no point in moving forward until this is figured out.

If this were a City based on the State chosen, I've found several options but haven't found the right one with this yet. Thanks gu开发者_StackOverflowys!


I don't think you're understanding (or taking advantage of) the model associations. Since I don't fully understand your model or business domain, I'm doing a lot of hand-waving here... it sounds like you want to restrict the number of options in the select box to just those that the current user should be able to see.

Let's say you had a Tumblr-style app. You have many users, and many blogs. The current logged in user can post a new item to any one of the blogs they have access to. When creating a new post and selecting which blog to publish it in, it would not be appropriate to let me post to your blog, or even know it exists. Instead of Blog.all, we'd use @current_user.blogs.

<%= semantic_form_for @post do |f| %>
  <%= f.input :blog, :label => "Which blog?", :as => :select, :collection => @current_user.blogs %>
  ...
<% end %>

I would see my list of blogs to choose from, you'd see yours. I think this partially answers your question.

The other part of the question seems to be around dynamically altering the contents of the select box based on the selections made in other select boxes in the form. This is something that would need to be handled with Javascript/AJAX, and kind of out-of-scope for Formtastic and a simple answer on Stack Overflow :)

I'd recommend keeping this as simple as possible for a first version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜