Rails 3.1.3

I have succeeded to 'create' a new entry using jQuery Ajax.

If users create new entries from

<div id="script_new">
<%= form_for script, :remote => true do |f| %>
<%= f.hidden_field :video_id %>
<%= f.text_field :startp, :readonly => true %>
<%= f.text_field :text %>
<%= f.submit "save"%>
<% end %>
</div>

, the newly created ones will show up in the list

<table>
<% for script in scripts %>
<tr>
<td><%= script.startp %></td>
<td ><%= script.text %></td>
<td><a href="#" class='edit_text' >edit</a></td>
<td><%= link_to 'Destroy', script, :class=>'small', :confirm =>
'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

I have wrote a JavaScript (jQuery) to add table elements to it.

Now my question is

How can I 'edit' (or 'update') the listed entries above?

Clicking the 'edit' button shown above will retrieve the corresponding
listed entry (script) from the table into the 'create' form. Well,
users may or
may not change the entry (script) there.
Then, naturally they click the 'save' button.

So I need a controller action that works as both 'save' and 'update'.

My 'create' action is follows.

def create
@script = Script.new(params[:script]) #HERE!!!!!!!
respond_to do |format|
if @script.save
format.json { render json: @script, status: :created, location:
@script,
notice: 'Script was successfully created.' }
else
format.html { render action: "new" }
format.json { render json: @script.errors, status:
:unprocessable_entity }
end
end
end

Script object calls for a new instance there. So I'm guessing that
it'll need to be

@script = Script.find(params[:id])

depending on the condition. But I have no clue from here to achieve
'save' or 'update' action.

Could anyone give me tips?

Regards.

soichi

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 13, '12 at 12:44a
activeFeb 13, '12 at 12:44a
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Soichi Ishida: 1 post

People

Translate

site design / logo © 2021 Grokbase