Forum: Ruby on Rails PLEASE HELP!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1f6d7d92019770f8782ded29f163dd59?d=identicon&s=25 Daniel Potter (dannypotta)
on 2005-11-30 13:47
Hi,

   I'm very new to Ruby and to Rails and I'm trying to get to grips with
it.   One of things I'm trying to do is to build a very simple web
application where, essentially, people can look up films and post
reviews about the films.

   I've created a database in MySQL with a Reviews table which includes
table columns: id, review_text, person_id and film_id.   id is the
primary key, Person_id and film_id are foreign keys that I'm trying to
pass into that table.

   Can anyone please, please tell me what syntax I need to use in the
VIEW in order to pass all of the data to the controller and save it in
the database?   Some of my code is below - I haven't yet built a person
login so I've simply assigned a person_id.   I've seem to have tried
every random bit of syntax I can think of and review_text gets submitted
but I can't pass person_id or film_id so it won't save in the database.


EXTRACTED CODE FROM VIEW

<%= @film.id %>
<%= @person.id = 22 %>

	<%= start_form_tag :action => "create_review", :id => @film %>
		<p>&nbsp;</p>
		<p><b>Review it:</b></p>
		<p><%= text_area "review", "review_text" %></p>
		<p>&nbsp;</p>
		<%# "film_id" = @film.id %>
		<%# "person_id" = @person.id %>
		<%= submit_tag "create" %>
	</form>


EXTRACTED CODE FROM CONTROLLER

def add_review
    #@review = Review.new (params[:review])
    @person = Person.new
    @film = Film.find(params[:id])
end

def create_review
    @review = Review.new(params[:review])
    if @review.save
      flash[:notice] = 'review was successfully created.'
      redirect_to :action => 'view_review'
    else render :action => 'new_review'
    end
 end
F85e4d039cb54389389010fc095b6b6a?d=identicon&s=25 moosebrookfarm (Guest)
on 2005-11-30 14:20
(Received via mailing list)
Hi Daniel -

For starters I don't see in your form how your film_id and person_id
are going to get passed into the action since they're not form fields
(and they're commented out).    Your scenario is kind of half baked at
the moment but to help in getting it working at the minimum you should
have those two id fields stored in a <input type="hidden" />  field
in rails you do this with:
hidden_field(:variable, :attribute, options)

Also I'm not sure why you're start_form_tag has an :id => @film in it.
  That's not doing anything but I'm not sure what you were trying to
do.

I would also suggest that you actually create the model objects that
you're going to be using in the real world so you're not creating
problems due to an unimplemented model.
script/generate scaffold will make this very easy to test.

Mike
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 JanPrill (Guest)
on 2005-11-30 14:24
(Received via mailing list)
Hi, Daniel,

IMHO you're approaching this thing from the wrong side. Why are you
working on the review-component while your film-component seems to be
not up at all? In your design a film seems to be the central model. You
should model a film- and a review-model as it is explained on the
community sites or in Agile Web Development with rails. A film then
should have a one to many relationship to the review-model. Once you've
got this up you might create some test-data through the scaffolded
admin-view and then you might add reviews to that film. This is exactly
the same as DHH does in the introduction video with articles (~films)
and comments (~reviews), so just have a look at that video and you
should get the idea.

regards
Jan
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2005-11-30 14:28
(Received via mailing list)
On 30.11.2005, at 15.19, Michael Engelhart wrote:
> Also I'm not sure why you're start_form_tag has an :id => @film in it.
>   That's not doing anything but I'm not sure what you were trying to
> do.

Actually, it does. It passes the id of the @film to the receiving
action so that it can be used as params[:id]. This all assuming that
the @film is a valid and saved Film object. So when using this you
won't have to use a hidden field for the film id.

//jarkko
F297fbb2a5538053e871a7707c304675?d=identicon&s=25 johncraigcole (Guest)
on 2005-11-30 14:36
(Received via mailing list)
Couldn't the form tag be written

<%= form_remote_tag :url => { :action => :someControllerMethod,
:filmid=>@film.id,  :personid=>@person.id }%>

That should allow you to refer to params[:filmid] and params[:personid]
in the controller.

John
5cfd7e9223637ea26c81c2cc3e43799e?d=identicon&s=25 craig (Guest)
on 2005-11-30 15:29
(Received via mailing list)
Michael beat me to the answer, but I'd just like to throw in an extra
two pieces of advice:

1) Email subject lines should describe the subject of your email. A
subject like "PLEASE HELP!" might not get read by the right person
where a subject like "How do I pass data from views to controllers?"
would.

2) Use <%= end_form_tag %> to close your form.

Yours,
Craig
--
Craig Webster | t: +44 (0)131 516 8595 | e: craig@xeriom.net
Xeriom.NET    | f: +44 (0)709 287 1902 | w: http://xeriom.net
1f6d7d92019770f8782ded29f163dd59?d=identicon&s=25 Daniel Potter (dannypotta)
on 2005-11-30 16:37
   Thanks very much all.   I think I'll watch the video first and then
try each of the various suggestions in turn.   As I said, I'm very new
to rails and still have very little clue what I'm doing but I guess I'll
learn by trying.

Thanks again,
Danny
This topic is locked and can not be replied to.