On Jueves, 17 de Junio de 2010 10:05:22 stephen murdoch escribió:
> I have a Post model and my controller looks like this:
>
> class Admin::PostsController < Admin::AdminController
>   active_scaffold :posts do |config|
>     config.columns = [:title, :body, :user_id]
>     config.list.columns = [:title, :body, :user_id]
>   end
> end
>
> Each Post belongs to one User, each User has many Posts.  Pretty
> basic.
>
> First Question:  Using the code above, when I go to create or edit a
> post, the user_id appears as an input field in the form and the poster
> actually has to manually enter their user_id in order for the
> association to be written to the database.  How can I do this
> automatically.  I know how to remove the user_id field and label from
> the form.  Do I have to do this in my model?  Like a before_create
> filter or something?

You can use before_create in the model, becuase ActiveScaffold enables you to 
use current_user in models, or you can define before_create_save in the 
controller:
def before_create_save(record)
  record.user = current_user
end


>
> Second Question:  I want to allow admins to be able to change the
> user_id for each post.  This would best be accomplished by having a
> drop-down select menu that is greyed out unless the user is an admin.
> Again, I think I can figure out how to grey the select-box out for
> admins, but I am having trouble getting the user_id to appear in a
> select box in the first place.  From reading the docs, I was under the
> impression that the following code might work, but it just throws up a
> noMethodError (which I would be happy to post, if anyone needs to see
> it):
>
>     config.columns[:user_id].form_ui = :select
> I also tried this code using "record_select" instea of just "select"

select form_ui must be used with association columns (:user) instead of 
foreign keys (:user_id). Usually ActiveScaffold uses associations instead of 
foreign keys.


-- 
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) [email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en.

Reply via email to