I'm using the Vestal Version Fork here for Rails 3 support: http://github.com/adamcooper/vestal_versions/
The issue I'm having is that the updated_by => current_user is not storing in the versions table on update.
def update
#book = Book.find(params[:id])
respond_to do |format|
if #book.update_attributes(params[:book].merge(:updated_by => current_user))
format.html { redirect_to(#book, :notice => 'Book was
successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #book.errors, :status => :unprocessable_entity }
end
end
end
I checked the logs, there is no error, Rails is INSERTING NULLs in the DB for the user fields, the one I need to populate is user_id
Ideas?
Thanks
Add the following to your model class should make it work
attr_accessible :updated_by
Related
I am running rails 3.2
I have created a nested form (requests > tags) with coffeescript handling the addition of new tags.
Everything works with the exception of the form posting a blank tag.name
I am trying to write a method to delete the blank field before the form posts. I realize this may be the wrong approach, but I am still a beginner:
requests_controller.rb
def create
#request = current_user.requests.build(params[:request])
#tag = Tag.new
if #tag.name.blank?
destroy_blank
end
respond_to do |format|
if #request.save
format.html { redirect_to(#request,
:notice => 'Request was successfully created.') }
format.json { render :json => #request,
:status => :created, :location => #request }
else
format.html { render :action => "new" }
format.json { render :json => #request.errors,
:status => :unprocessable_entity }
end
end
end
request.rb
def destroy_blank
blank = #tag.name
blank.delete
end
I hope that's clear. If not let me know and I will include more information.
If you can't stop blank tags from coming in, you can create a before_create filter in the model to skip saving blank tags. Leave the controller clean and simple.
Good luck!
Is there a difference between
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render :new
end
end
and
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to(:users, :notice => 'Registration successfull. Check your email for activation instructions.') }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
Ignore the error and notice issues, my main question is the difference between using xml format and not using it, they seem to do the exact thing.
Using respond_to with different format than html give you the ability to have the response in the specified format (useful for web-service).
In that case (User creation) I don't think it is really useful, but it's all up to you!
Not using respond_to like your first exemple will simply render html.
More infos about respond_to here:
http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
I am still a bit new to RoR, and am using scaffolding to generate CRUD interfaces for data.
I am using devise for user authentication, and want to allow a user that owns a specific entry to edit or delete, but protect that data from other users. However, I would like to allow a different user to revise or create new versions.
So if a user that attempts to edit should appear as if they are editing, but when they submit, the controller should actually generate a new entry (and potentially specify the parent_id of the entry it derived from).
Any help on implementation is greatly appreciated.
Also look into Ancestry, it's a really nice library to help with versioning.
Here is my solution that I came up with. I am a ruby newb, so I assume that I can just call the create function with the same params but wasn't sure how to do that, so I just duplicated code:
def update
#section = Section.find(params[:id])
if #section.owner == current_user.id
respond_to do |format|
if #section.update_attributes(params[:section])
format.html { redirect_to(#section, :notice => 'Section was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #section.errors, :status => :unprocessable_entity }
end
end
else
# REVISE
#childsection = Section.new(params[:section])
respond_to do |format|
if #childsection.save
format.html { redirect_to(#childsection, :notice => 'Section was successfully revised.') }
format.xml { render :xml => #childsection, :status => :created, :location => #childsection }
else
format.html { render :action => "new" }
format.xml { render :xml => #childsection.errors, :status => :unprocessable_entity }
end
end
end
end
I'm diving into RoR and as I'm going through the tutorials, scaffolds, and docs, I'm coming across some code that confuses me. For example, I just read up on the 'redirect_to' method, but the guide I read didn't cover the example of redirecting to an instance var, such as the code that is generated in a typical scaffold...
# POST /articles
# POST /articles.xml
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to(#article, :notice => 'Article was successfully created.') }
format.xml { render :xml => #article, :status => :created, :location => #article }
else
format.html { render :action => "new" }
format.xml { render :xml => #article.errors, :status => :unprocessable_entity }
end
end
end
In the statement format.html { redirect_to(#article, :notice => 'Article was successfully created.') }, the code is redirecting to the instance var article, which causes a 'redirect_to' the show method in the current controller. Why does this cause it to redirect to the show method?
Thanks so much for your help!
Why does this cause it to redirect to the show method?
Because if you don't specify particular action, Rails assumes you want to 'show' object. If you have another action in mind, try
redirect_to :action => :do_something, :id => #article
I'm using Rails 3.0.0.rc with Ruby 1.8.7 on Snow Leopard. I was using Builder to to restrict what's returned by render :xml for a User model object. It wasn't working as expected, so I commented out my format.xml block.
E.g., this was my users_controller.rb:
def create
# TODO: Limit the frequency at which this can be called.
#user = User.new
#user.first_name = params[:user][:first_name]
#user.last_name = params[:user][:last_name]
# etc...
respond_to do |format|
if #user.save
format.html { redirect_to(#user, :notice => 'User was successfully created.') }
format.xml { render :xml => #user, :status => :created, :location => #user }
else
format.html { render :action => "new" }
format.xml { render :xml => #user.errors, :status => :unprocessable_entity }
end
end
end
I changed the first format.xml and commented out the block:
format.xml # { render :xml => #user, :status => :created, :location => #user }
When I removed my comment and reintroduced the block, I get an error:
ArgumentError (wrong number of
arguments (1 for 0)):
app/controllers/users_controller.rb:80:in
create'
app/controllers/users_controller.rb:77:in
create'
I reformatted the code so the render :xml call is on its own line, just to confirm that this is the source of the problem.
What's going on? There are three arguments involved (not 1) and since when does render take zero arguments? Why did the error only appear after I reverted the code to its original state?
Turns out the error wasn't being reported properly. I had an error in my Users::to_xml method.